addressof object with id()
steve+comp.lang.python at pearwood.info
Sun Mar 24 02:52:30 CET 2013
On Sat, 23 Mar 2013 19:37:35 -0500, Fabian von Romberg wrote:
> I have a single questions regarding id() built-in function.
> example 1:
> var1 = "some string"
> var2 = "some string"
> if use the id() function on both, it returns exactly the same address.
The id() function does not return an address. Scrub that from your mind.
See below for further details.
In any case, when I try the above, I do *not* get the same ID.
py> var1 = "some string"
py> var2 = "some string"
py> id(var1), id(var2)
I get two distinct IDs.
> example 2:
> data = "some string"
> var1 = data
> var2 = data
In this case, id(var1) and id(var2) is *guaranteed* to return the same
value, because both names are bound to the same object.
The first line creates a string object and binds it to the name "data".
The second line binds the same object to the name "var1", and the third
line does the same to the name "var2". So now you have three names all
referring to the same object. Since all three names refer to the same
object, it doesn't matter whether you call id(data), id(var1) or
id(var2), they return the same ID.
Objects in Python are like people -- they can have more than one name, or
no name at all. Depending on who is doing the talking, all of the
following could refer to the same person:
> if use the id() function on var1 and var2, it returns exactly the same
The id() function does not return an address. It returns an arbitrary
identification number, an ID. In Jython, IDs start at 1, and are never re-
used. In CPython, IDs look like addresses, and may be re-used.
The only promises that Python makes about IDs are:
- they are integers;
- they are constant for the life-time of the object;
- they are unique for the life-time of the object.
That means that they *may* or *may not* be re-used. Jython does not re-
use them, CPython does.
Python does not make many promises about object identity. Creating a new
object using a literal expression may or may not create a new object.
Python makes no promises either way. For example:
py> a = 42
py> b = 42
py> id(a) == id(b) # a and b are the same object
py> a = 420000
py> b = 420000
py> id(a) == id(b) # a and b are different objects
py> x = id(420000)
py> y = id(420000)
py> x == y # x and y get the same ID
However all of these examples are implementation dependent and may vary
according to the Python implementation and version. In particular, the
third example is specific to CPython. In Jython, for example, the IDs
will be unequal.
id() is one of the most pointless functions in Python. There is almost
never any need to care about the id of an object. I wish that it were
moved into the inspect module instead of a built-in function, because in
practice id() is only useful for confusing beginners.
 They look like addresses because the CPython implementation uses the
address of the object, as returned by the C compiler, as the ID. But
since there is no way to dereference an id, or lookup the object found at
an id, it is not an address. It merely has the same numeric value as what
the C compiler sees as the address.
More information about the Python-list