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:
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()?
-
7Most of the details you're asking about, you should completely ignore for Python programming. Python isn't defined in terms of memory or pointers or low-level things like that. (The implementation needs to worry about that stuff, but you don't need to worry about the implementation.)user2357112– user23571122014年03月24日 09:11:26 +00:00Commented Mar 24, 2014 at 9:11
-
@user2357112 For your statement"you should completely ignore for Python programming", One cannot avoid such questions from people coming from languages of OOP paradigm like Java or imperative paradigm like C. As Noel is saying below that python is dynamically & strongly typed, so it make sense to understand type things applied on literals & functions(in runtime). After i start use python for 2-3 years and i do not know whether op is function pointer or just a reference variable of some class, I feel this is not proper approach in learning a language.overexchange– overexchange2014年03月25日 06:55:19 +00:00Commented Mar 25, 2014 at 6:55
-
I feel, declarative paradigm languages(like SQL) does not encourage user to learn inner details.overexchange– overexchange2014年03月25日 07:03:26 +00:00Commented Mar 25, 2014 at 7:03
-
1When you program in C, you think in terms of memory and pointers, because those things are fundamental to C. Unless you really need to, you don't think about which register stores which variable or which layer of cache you're hitting. Those issues are handled for you by the implementation. Similarly, in Python, you think in terms of objects and their behavior. You don't need to worry about memory and pointers, because at Python level, those are implementation details.user2357112– user23571122014年03月25日 07:48:24 +00:00Commented Mar 25, 2014 at 7:48
-
4I removed the Java tag, it has no relevance to Java. It doesn't really have relevance to C either, but I kept the tag because you're trying to compare Python's memory model to C's. Still, I gave your question a -1 because it is not useful to try to understand Python programs in terms of how they map to C's memory model. Instead, try to learn about how to write a Python interpreter in C (e.g. by getting the source code from python.org).Guido van Rossum– Guido van Rossum2014年03月25日 15:09:08 +00:00Commented Mar 25, 2014 at 15:09
3 Answers 3
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.
21 Comments
>>> (2).__add__(3) 5In 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.
constis 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,constis 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
ophas the functionsubas a value.
3 Comments
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
consta name of 32/64 bit memory area storing the value2with 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
opa function pointer pointing to functionsub()?
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).
22 Comments
const just refers to an object of class int. 2. Yes, in general everything is a reference in Python, but I don't see a contradiction. (Besides, a function may be "wrapped in a class" (bound to it), but doesn't have to.) 3. This seems to be right.