Search notes:

Python: id

id(obj) returns an object's identity.
In CPython, id() returns the address of the object in memory.

type

id() returns an int:
#!/usr/bin/python3

obj = [1, 2, 3, 4, 5]

print(type(id(obj))) # <class 'int'>
Github repository about-python, path: /builtin-functions/id/type.py

id(id)

Of course, the function id has an id, too:
#!/usr/bin/python3

print(id(id))
Github repository about-python, path: /builtin-functions/id/id_id.py

id(42)

In CPython, there seems to be a memory location even for literals, such as 42 or 0.
In the following example, the id of 42 is 2012990016, the id of 0 is 2012989344.
>>> id(42)
2012990016
>>> forty_two=42
>>> id(forty_two)
2012990016
>>> num42=42
>>> id(num42)
2012990016
>>> id(0)
2012989344
>>> zero=0
>>> id(zero)
2012989344
>>> num0=0
>>> id(num0)
2012989344
How can that be?
The answer is found in the Python's C API reference manual on Integer objects:
The current implementation keeps an array of integer objects for all integers between -5 and 256, when you create an int in that range you actually just get back a reference to the existing object
The preallocation of the ids is found in the CPython source files in Inlcude/internal/pycore_interp.h.
So, with that information, we verify that the number 257 does not come with its «own» id:
>>> two_hundred_and_fifty_seven = 257
>>> num_257                     = 257
>>> id(two_hundred_and_fifty_seven)
17055472
>>> id(num_257)
17055504

Getting an object's reference counter via id()

Because in CPython, id() returns the memory-address where the object's reference counter is stored (see definition of Include/object.h. Include/object.h), id() can be used together with ctypes to get an object's reference counter.
How this might be done is outlined here.

See also

Multiple variables referring to the same object
Python: Built in functions

Index