My query is on the below program with respect to symbols that are storing values and functions, when ran on http://pythontutor.com/.
My question is:
How does python execution model look for above program on memory before start interpreting the python program? How do i visualise that memory layout? for example c executable has code/stack/heap/extra/data segments, just as an example, am not comparing
Is 'const' a name of 32/64 bit memory area storing the value 2 with type assigned as integer?
add()/sub()/other functions are shown in Objects column as per the diagram, So, How do i perceive functions being stored as Objects? How do i visualise it?
As per the diagram, Is op a function pointer pointing to function sub()?
In Python, don't worry so much about memory segments and what goes on behind the scenes. Rather, environments (scopes) are more important. The block-and-pointer diagram you included is a reasonable way to visualize the memory. The white portion shows what the global environment looks like. When the function is called, a new (blue) environment is created.
const
is a variable. Variables in Python areweaklydynamically typed, and can store anything. In fact, Python integers don't overflow, and can store numbers exceeding 264. In this case,const
is a variable (with a confusing name) that contains the number2
.A function is an abstract notion of a callable blob of code. You can assign it to a variable just like any other value.
You could consider it a function pointer if it makes you feel comfortable, but then you would be outing yourself as a C programmer. A Python programmer would just say that
op
has the functionsub
as a value.It has a kind of layout, but here the heap is the most important part, as every object is put to the heap. The code segment is merely the interpreter, the data segment is as well internal state of the interpreter, and the stack as well.
What is relevant for the Python program is only the heap. But the layout itself is like any other program.
It is a name in the current working space, (here: in the module's namespace), which is essentially a dict which makes assignments between strings and arbitrary objects. In this case, it makes the string
const
refer to an integer object which holds the value2
. (This object can be created newly or re-used depending on the circumstances; this makes no difference, as it is immutable.)As written in my comments to Ignacio's answer:
In the case of functions, you have an object which has certain fields, which contain e. g. the code in terms of bytecode, the number of parameters it has, etc. And it even has methods itself, for example
__get__()
which is called internally for binding a method to an object, or__call__()
for the real function call, besides__format__()
,__repr__()
etc.An integer object has, somewhere deep inside, a place for storing the actual value. In the case of a
long()
in Py2, or anyint()
in Py3, it stores the data to hold the value (e. g.2
) and as well the length needed for it. Besides, it has a number of methods. Have a look at the output ofdir(2)
to see it having a bunch of methods as well, such as for formatting, for arithmetics, etc.Kind of, yes.
There is a function object, which internally knows that its original name was
sub
. But this knowledge is only for displaying purposes.In your case, it is referred from two names,
op
andsub
. So referring to either of them has the same result.Note that there are no "function pointers" per se, there are just references, or names, which refer to an object of any type. The type of an object is fixed, but not the "type of a reference" (as there is no such thing).
Dictionaries on dictionaries. Dictionaries are the number 1 most important structure in Python.
It is the key of an entry in the current scope's dictionary. The value is the object
2
.It is not that functions are objects, but that some objects are functions. Or numbers. Or dictionaries.
It is the key of an entry in the current scope's dictionary. The value is
sub
.