Address of an immutable object

candide candide at free.invalid
Sun May 30 13:38:23 EDT 2010


Suppose a Python program defines an integer object with value 42. The 
object has an "address" we can capture with the built-in function id() :

 >>> a=42
 >>> id(a)
152263540
 >>>

Now I was wondering if any integer object with value 42 will be refered 
at the same adress with the above id.

Some experiments tend to show that it may be the case, for instance :

 >>> a=42
 >>> id(a)
152263540
 >>> id(42)
152263540
 >>> b=2*21
 >>> id(b)
152263540
 >>> c=0b101010
 >>> id(c)
152263540 

 >>> d={"foo":42, "bar":"foo"}
 >>> id(d["foo"])
152263540
 >>> L=["foo",(51,([14,42],5)),"bar"]
 >>> id(L[1][1][0][1])
152263540
 >>> del a
 >>> id(L[1][1][0][1]) 

152263540 

 >>> zzz=range(1000)
 >>> id(zzz[42])
152263540
 >>>

Even you can't make a deep copy :


 >>> from copy import deepcopy
 >>> a=42
 >>> from copy import deepcopy
 >>> z=deepcopy(a)
 >>> id(a), id(z)
(152263540, 152263540)
 >>>

So is the following true :

Two non mutable objects with the same value shall be allocated at a 
constant and unique address ?



More information about the Python-list mailing list