id() returns the memory address, which can be reused after an object is garbage collected
id() returns an integer that is guaranteed to be unique for the lifetime of an object. In CPython, this is the object's memory address. When an object is no longer referenced, it is garbage collected and its memory can be reused for a new object. If you create a new object after the old one is collected, it may occupy the same memory address, so id() returns the same value. This is not a bug; it is a consequence of memory reuse. You can observe it with temporary objects, small integers that are cached, or objects created and discarded quickly. The important rule is never to use id() as a persistent identifier or to assume uniqueness across time.
id() is unique only while the object is alive. After collection, the address can be reused.
Small integers (-5 to 256) are interned and live forever, so their ids are stable and shared.
String interning can also cause ids to match for equal strings in some cases.
Never use id() as a key in a persistent store or as a unique identifier across process boundaries.
Common mistake: assuming two objects with the same id() are the same object when they are not alive at the same time.
Common mistake: using id() to compare objects; use == or is instead.
Version note: id() behavior is CPython implementation detail; other implementations may use different schemes.
0-2 years experience
2-5 years experience
5-8 years experience
8+ years experience