Python Forum
how operators are implemented as function calls
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how operators are implemented as function calls
#1
i remember a thread a while back that touched on how various operators were implemented as funtion calls. i'm more curious about this, now, but i cannot find the thread. so i'd like to know what document i can read that describes this. one of the things i'm curious about is how assignments differ from data fetches in, for example, a dictionary. if i do = mydict[x] vs. mydict[x] = how do the function calls differ. is this an implementation decision?

edit:

i'd imagine it could be just one function that with just one argument means to fetch data indexed by that argument, and with two arguments means to store the data (given in the 2nd argument) at the specified index.

edit:

i've noted that a few posts show that some of you know about expressing an assembly-like language that expresses what goes on inside Python (or at least inside CPython). where did you learn this? is there a document that explains all of this?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Skaperen Wrote:i've noted that a few posts show that some of you know about expressing an assembly-like language that expresses what goes on inside Python (or at least inside CPython).
Do you mean bytecode? Try the dis module
>>> import dis
>>> dis.dis("mydict[x] = y")
  1           0 LOAD_NAME                0 (y)
              3 LOAD_NAME                1 (mydict)
              6 LOAD_NAME                2 (x)
              9 STORE_SUBSCR
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE
>>> dis.dis("y = mydict[x]")
  1           0 LOAD_NAME                0 (mydict)
              3 LOAD_NAME                1 (x)
              6 BINARY_SUBSCR
              7 STORE_NAME               2 (y)
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE
Reply
#3
Are you talking about the operator module, or are you talking about special OOP methods for overloading operators (__add__, __sub__, __mul__, and so on)? Or are you talking about something else?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Dec-14-2018, 01:50 PM)ichabod801 Wrote: Are you talking about the operator module, or are you talking about special OOP methods for overloading operators (__add__, __sub__, __mul__, and so on)? Or are you talking about something else?
maybe many different things. for example, making my own class operate from operators. i don't know how operators end up making the calls. i am looking for where i can find this documented. i have briefed through the PDF documents, but have not found this. things a dictionary can do, that i would like for my class to do:

1. handle methods like obj.keys(), obj.values(), and obj.items(). this should simply be a matter of implementing those methods, as named.

2. handle indexed storing, like obj[foo] = bar. i don't know the name of the method that would be called.

3. handle indexed fetching, like bar = obj[foo]. i don't know if this is a different method than storing, or the same without the value argument.

4. handle things like len(obj) and str(obj). i read the article you wrote in the fundamentals forum that covered these in a quick and simple way. but i am still curious if there is a formal document for this, even if it is harder to read.

5. handle attribute references like obj.foo = bar, bar = obj.foo, and obj.foo(bar). the first 2 might be __setattr__() and __getattr__() while the 3rd might be simply implementing foo().

you might well know all the answers to these, but i really am wanting to know where i need to be looking. is it in documents i have and overlooked part? is it a different document? or is the only formal way to learn this to read implementation (CPython) source code?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
i just found this and it seems to be a good on-this-topic read:

http://getpython3.com/diveintopython3/sp...names.html
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
The data model section of the documentation also has a lot of info.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  optimizing calls to isinstance() Skaperen 0 844 Nov-27-2022, 01:33 AM
Last Post: Skaperen
  omitting arguments in function/method calls Skaperen 10 3,872 Nov-24-2019, 10:13 PM
Last Post: Gribouillis
  overload operators anjita 4 2,523 Jan-04-2019, 12:31 PM
Last Post: buran
  has anyone implemented a sort command in Python? Skaperen 10 5,244 Dec-20-2018, 05:44 AM
Last Post: Skaperen
  Ugh, Someone Went and Implemented Case ichabod801 2 2,708 Sep-15-2017, 09:46 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020