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?
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
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?
(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?