Return the “identity” of an object. This is an 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.
Source: https://docs.python.org/3.7/library/functions.html#id
There are situations that some values have the same id, because they are implemented as singletons. These are usually values that are statistically going to occur often in a typical program. Some obvious examples of this are None, True, and False.
>>> variable = True
>>> variable is True
True
Some non-obvious examples of this are relatively small numbers.
>>> variable = 256
>>> variable is 256
True
>>> variable = 257
>>> variable is 257
False
This is also why people advise using the ==
operator over is
for value comparison.