0

I run the below code in python interpreter

Python 2.7.12 (default, Sep 20 2016, 14:42:48)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 300
>>> y = 300
>>> id(x)
140510001982256
>>> id(y)
140510001982160

And then I write a small program and run it:

Program :

 x = 15000
 y = 15000

 if __name__ == "__main__":
     print (id(x))
     print (id(y))

Output:

$ python mem_test.py
140525354104776
140525354104776

What is the reason for this ?

  • It about understanding the behaviour of variable memory allocation.The problem is why a different behaviour in program and python interpreter. – poseidon_rishi Oct 14 '16 at 09:37
  • Those are different objects. The names `x` and `y` are only references to the objects. `x` and `y` are, say, cosmetic – Moses Koledoye Oct 14 '16 at 09:39
  • I understand that. Then in interpreter too they should be returning same reference right , but which ain't happening.I am just curious to know why.Please try the example I have mentioned , may be you get what I am trying to convey. – poseidon_rishi Oct 14 '16 at 09:41
  • In the program you can change 15000 to 300 and then run .You will see in interpreter both point to diff memory location but while running in program they point to same memory location – poseidon_rishi Oct 14 '16 at 09:44
  • Those are implementation dependent. It's not a given. See [identifying objects, why does the returned value from id(…) change?](http://stackoverflow.com/questions/3402679/identifying-objects-why-does-the-returned-value-from-id-change) – Moses Koledoye Oct 14 '16 at 09:52
  • See also http://stackoverflow.com/questions/17132047/same-value-for-idfloat – Moses Koledoye Oct 14 '16 at 09:55

3 Answers3

0

id(object)
Return the “identity” of an object. This is an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value

CPython implementation detail: This is the address of the object in memory.

from that description you can understand that id(Object) returns either unique constant that provides non overlapping space for a variable.

Deepak Tripathi
  • 600
  • 2
  • 4
  • 22
0

You are literately using the object location's identity in memory. Since integers are immutable, each integer value is a distinct object with a unique id. The integer 1500 has a different id from 1499.If you expand your variables, value 1500 is stored once and everything else is just pointing to that value which will reference to the memory location of number 1500.

For example:

x = 1500
z = x
f = z
y = 1500
n = 1499

if __name__ == "__main__":
     print (id(x))
     print (id(z))
     print (id(f))
     print (id(y))
     print (id(n))
>>> 
50864556
50864556
50864556
50864556
50733712
>>> 
Kian
  • 1,319
  • 1
  • 13
  • 23
0

An object id is an implementation-defined "blackbox" value. The only garantees are that a given object will keep the same id for all it's lifetime and that no other object (in the same process...) will share this id during this same time (but the id can be reused for another object in the same process one the first object has been garbage collected).

Note that the implementation is free to cache immutable objects for performance reasons, and CPython has indeed been caching some immutable objects under certain circonstances - notably "small" integers (for a definition of "small" that has changed from version to version) and strings that could be valid variables/functions/classes/modules names, but this is really just an implementation detail. FWIW, it looks like some of this caching is not happening at the REPL's top-level anymore (if it ever happened here) - but it still happens in functions defined in the REPL:

Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
pythonrc start
pythonrc done
>>> x = 300
>>> y = 300
>>> id(x)
37126296
>>> id(y)
37126248
>>> def foo():
...    x = 300; print "id(x): %s" % id(x)
...    y = 300; print "id(y): %s" % id(y)
... 
>>> foo()
id(x): 37126200
id(y): 37126200
>>> foo()
id(x): 37126200
id(y): 37126200
>>> foo()
id(x): 37126200
id(y): 37126200
>>> id(x)
37126296
>>> id(y)
37126248

To make a long story short: you shouldn't ever care about this (unless you're writing your own python runtime of course).

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118