3

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

6
  • 7
    Most 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.) Commented 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. Commented Mar 25, 2014 at 6:55
  • I feel, declarative paradigm languages(like SQL) does not encourage user to learn inner details. Commented Mar 25, 2014 at 7:03
  • 1
    When 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. Commented Mar 25, 2014 at 7:48
  • 4
    I 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). Commented Mar 25, 2014 at 15:09

3 Answers 3

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

answered Mar 24, 2014 at 9:23
Sign up to request clarification or add additional context in comments.

21 Comments

Ignacio, What is the meaning of these 2 statements: a) 'The value is the object 2' b) 'some objects are functions. Or numbers. Or dictionaries.'??---How do you define word 'object' here? Because, In OOP world, object has state & behaviour.
Why do you believe that objects in Python don't have state and behavior?
I didn't say, objects in python don't have state & behaviour, But As per link I understand that, Software objects stores its state in fields (variables in some programming languages) and exposes its behaviour through methods (functions in some programming languages). ------But in your update, In point 2, You say that an integer literal(2) is an object, In point 3, You say a function add()/sub() is an object.
That's because they are.
Same way everything else is an object. >>> (2).__add__(3) 5
|
3
  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.

answered Mar 24, 2014 at 10:13

3 Comments

"Variables in Python are weakly typed" - I'd rephrase it. Variables aren't typed at all, but values are typed (strongly typed).
Python is not weakly-typed like Perl or C are. It's dynamically, but strongly-typed.
@Kos Do you meant, language agnostically, one should not say, variables are weakly/strongly typed, as it does not make sense? Do we need to say, values are weakly/strongly typed?
2

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

answered Mar 24, 2014 at 12:43

22 Comments

glglgl Wrt your second point, i see a point, i think, integer literal(2) is auto boxed to integer class(at runtime)and const will be of type integer class in runtime. In general, any literal "abc" , 3.2 will be autoboxed to corresponding class in runtime during the assignment time of 'const = 2', so const will be of type integer class in runtime.
glglgl Wrt your third point, i dont agree with you, i feel a function(say sub() in above example) is also wrapped within a class and then pointed by a reference variable(say op in above example), where op will be of that class type in runtime. for 3rd point, we do this in java as well 'interface Sample { int sub(int x, int y); }' or 'class Sample { int sub(int x, int y){return x-y;} }' so i think const is a reference variable of type integer class above(in runtime) & op is a reference variable(not a function pointer) of type class that sub() is wrapped into.
glglgl and when i say reference variable in python, i think the behaviour of this reference variable is same as in java(in runtime) except that in java type of this reference variable is known at compile time(which is X) and Y in runtime as per example 'X x = new Y();' where X & Y are classes Y is child of X and x is a reference variable
1. Maybe you call it autoboxing if you come from Java, I'd rather call it "automatic object creation (and interning)". But 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.
glglgl Wrt your statement in point 2 above: 'Besides, a function may be "wrapped in a class" (bound to it), but doesn't have to.'----- If you don't wrap into a class, then what is the type of reference variable op in runtime?
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.