12

In the below diagram, I have a query on name temp that is returned from function f

enter image description here

In local frame f, temp is a reference variable pointing to an object 6 of class int, when you say return temp in f a reference variable output which is part of checkPassingMech frame will point to the same object 6 that temp was pointing to.

My question:

Q1) Is my understanding correct?

Q2) If Q1 is yes, then this diagram is giving an illusion that temp is not reference type and showing its value in a box rather than with an arrow pointing to 6, am i correct?

Q3) If Q2 is yes, then, Can i say that 6 is actually stored in heap and temp and output would be pointing to this heap space from frame(local stack)

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • Correct. Such diagrams usually take the shortcut of treating simple values like small integer literals as discrete objects, rather than references to objects, to avoid cluttering the diagram. – chepner Apr 06 '14 at 15:52
  • @chepner what kind of parameter passing mechanism is this? the way temp is returned, because in c, we cant do this, we get into dangling pointers, because value is stored in local stack in c. I know this is compiled lang, but we can compare param passing mechanism. – overexchange Apr 06 '14 at 16:28
  • Python doesn't have pointers; it just has references. Memory allocation is solely the job of the interpreter. At the Python level, only names are bound to particular scope, and it doesn't matter to the memory management system whether existing references are stored in global or local names, as long as at least one reference exists to an object. – chepner Apr 06 '14 at 16:45
  • @chepner for your point, pointers are not references,'int a=3;int *ptr=&a' in c, 'X x = new X();' in java, 'temp = 6' in python, dont u think, ptr x temp are storing the address of memory where the value is stored? – overexchange Apr 06 '14 at 17:02
  • How references are implemented are entirely up to the Python implementation. They might be implemented by pointers, they might not. The implementation is not relevant to your question. – chepner Apr 06 '14 at 18:15
  • @chepner Instead of returning single value of 'temp' in function 'f', if i return something like 'return floordiv(n, d), mod(n, d)' (say), Does that return multiple values stored in heap? When i say, multiple, i have two values here seperated by commas in my example. How does this work? – overexchange Apr 07 '14 at 01:40
  • The function only returns one value: a reference to a tuple that contains references to two other objects. – chepner Apr 07 '14 at 03:00
  • @chepner i want to accept an answer of yours, How do i? If you have time, Can you frame in the form of answer? I don't want your effort not counted. – overexchange Apr 07 '14 at 04:34
  • @chepner one unrelated query to add for my existing query is, does javascript also behave in the same way as shown in the diagram? Because in javascript, everything is an object. – overexchange Apr 07 '14 at 18:18
  • I suspect it is similar, although I have virtually no experience with that language. – chepner Apr 07 '14 at 18:23

4 Answers4

9
>>> def f():
    temp = 6
    print(id(temp))
    return temp

>>> output = f()
507107408
>>> id(output)
507107408

from doc:

CPython implementation detail: For CPython, id(x) is the memory address where x is stored.

So strictly speaking, if the value is 6, then output and temp is actually pointing to the same object, which is an int object that is cached when python started.

You may refer to identifying objects, why does the returned value from id(...) change? for more information.

Community
  • 1
  • 1
laike9m
  • 18,344
  • 20
  • 107
  • 140
1

Yes, your understanding is correct, besides Python's runtime only deals in references to objects (which all live in the heap): what goes on Python's stack (as operands and results of its bytecode operations) are always references (to values that live elsewhere).

All Python objects in the CPython implementation go on the heap. You can read in detail how Python's memory management works here in the documentation:

Memory management in Python involves a private heap containing all Python objects and data structures. The management of this private heap is ensured internally by the Python memory manager. The Python memory manager has different components which deal with various dynamic storage management aspects, like sharing, segmentation, preallocation or caching.

ajknzhol
  • 6,322
  • 13
  • 45
  • 72
  • `what goes on Python's stack (as operands and results of its bytecode operations) are always references (to values that live elsewhere)`, will you explain more about it or reference? – laike9m Apr 07 '14 at 03:23
1

"Types affect almost all aspects of object behavior. Even the importance of object identity is affected in some sense: for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed. E.g., after a = 1; b = 1, a and b may or may not refer to the same object with the value one, depending on the implementation, but after c = []; d = [], c and d are guaranteed to refer to two different, unique, newly created empty lists. (Note that c = d = [] assigns the same object to both c and d.)"

https://docs.python.org/2/reference/datamodel.html

So you wouldn't be able to just put an arrow pointing at 6, because it is not guaranteed to be the same reference to 6 each time.

flakes
  • 21,558
  • 8
  • 41
  • 88
1

1) Yes. 2) Yes. That is true, temp is not a reference type because it is not referring to any other variables. 3) Yes, the value six is stored in the heap(in this scenario) and temp would be pointing at six no matter the input and the output equals temp which makes it equal to 6.

You can get a good definition on reference types in this link: http://msdn.microsoft.com/en-us/library/490f96s2.aspx

elishaolade
  • 75
  • 12