I read the Python 2 docs and noticed the id()
function:
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.
So, I experimented by using id()
with a list:
>>> list = [1,2,3]
>>> id(list[0])
31186196
>>> id(list[1])
31907092 // increased by 896
>>> id(list[2])
31907080 // decreased by 12
What is the integer returned from the function? Is it synonymous to memory addresses in C? If so, why doesn't the integer correspond to the size of the data type?
When is id()
used in practice?
id()
does return the address of the object being referenced (in CPython), but your confusion comes from the fact that python lists are very different from C arrays. In a python list, every element is a reference. So what you are doing is much more similar to this C code:In other words, you are printing the address from the reference and not an address relative to where your list is stored.
In my case, I have found the
id()
function handy for creating opaque handles to return to C code when callingpython
from C. Doing that, you can easily use a dictionary to look up the object from its handle and it's guaranteed to be unique.The
is
operator uses it to check whether two objects are identical (as opposed to equal). The actual value that is returned fromid()
is pretty much never used for anything because it doesn't really have a meaning, and it's platform-dependent.That's the identity of the location of the object in memory...
This example might help you understand the concept a little more.
These all point to the same location in memory, which is why their values are the same. In the example,
1
is only stored once, and anything else pointing to1
will reference that memory location.I am starting out with python and I use id when I use the interactive shell to see whether my variables are assigned to the same thing or if they just look the same.
Every value is an id, which is a unique number related to where it is stored in the memory of the computer.
Rob's answer (most voted above) is correct. I would like to add that in some situations using IDs is useful as it allows for comparison of objects and finding which objects refer to your objects.
The later usually helps you for example to debug strange bugs where mutable objects are passed as parameter to say classes and are assigned to local vars in a class. Mutating those objects will mutate vars in a class. This manifests itself in strange behavior where multiple things change at the same time.
Recently I had this problem with a Python/Tkinter app where editing text in one text entry field changed the text in another as I typed :)
Here is an example on how you might use function id() to trace where those references are. By all means this is not a solution covering all possible cases, but you get the idea. Again IDs are used in the background and user does not see them:
OUTPUT:
Underscores in variable names are used to prevent name colisions. Functions use "fromwhere" argument so that you can let them know where to start searching for references. This argument is filled by a function that lists all names in a given namespace. Globals() is one such function.
The answer is pretty much never. IDs are mainly used internally to Python.
The average Python programmer will probably never need to use
id()
in their code.