Python memory model for this program

2020-02-01 06:45发布

My query is on the below program with respect to symbols that are storing values and functions, when ran on http://pythontutor.com/.

memory model

My question is:

  1. 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

  2. Is 'const' a name of 32/64 bit memory area storing the value 2 with type assigned as integer?

  3. 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?

  4. As per the diagram, Is op a function pointer pointing to function sub()?

标签: python c object
3条回答
走好不送
2楼-- · 2020-02-01 07:17
  1. 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.

  2. const is a variable. Variables in Python are weakly dynamically 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 number 2.

  3. A function is an abstract notion of a callable blob of code. You can assign it to a variable just like any other value.

  4. 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 function sub as a value.

查看更多
手持菜刀,她持情操
3楼-- · 2020-02-01 07:27

Every C (compiled language) program has code/data/stack/extra/heap segments loaded in memory before execution. Does python interpreter create any memory layout for every python program before start interpreting the python program? If yes, How do i visualise that memory layout?

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.

Is const a name of 32/64 bit memory area storing the value 2 with type assigned as integer?

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 value 2. (This object can be created newly or re-used depending on the circumstances; this makes no difference, as it is immutable.)

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 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 any int() 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 of dir(2) to see it having a bunch of methods as well, such as for formatting, for arithmetics, etc.

As per the diagram, Is op a function pointer pointing to function sub()?

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 and sub. 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).

查看更多
▲ chillily
4楼-- · 2020-02-01 07:28
  1. Dictionaries on dictionaries. Dictionaries are the number 1 most important structure in Python.

  2. It is the key of an entry in the current scope's dictionary. The value is the object 2.

  3. It is not that functions are objects, but that some objects are functions. Or numbers. Or dictionaries.

  4. It is the key of an entry in the current scope's dictionary. The value is sub.

查看更多
登录 后发表回答