Check whether two references point to the same object
Debugging references
Python · Function
id() returns an integer identity for an object. It remains unique and constant during that object's lifetime, but values may be reused by objects whose lifetimes do not overlap.
Call id() with one object to obtain its identity as an integer. That value remains constant and unique for that object during its lifetime, although a later object may receive the same value after the original object no longer exists.
id(object, /)| Name | Type | Description |
|---|---|---|
object | object | The object whose identity is returned. |
An integer representing the object's identity, guaranteed unique and constant during that object's lifetime.
builtins.id auditing event.value = []
print(id(value) == id(value))
The same object produces the same identity value while it remains alive.
Debugging references
Object lifecycle diagnostics
Auditing
id() return?id() accepts one object and returns an integer representing that object's identity. The value is guaranteed to stay constant and be unique for that object while the object is alive.
Follow-up: Can two different objects have the same identity value? Yes, if their lifetimes do not overlap, because the value may be reused.
The result identifies the object itself, not merely an equal value. Two separate objects can compare equal while still having different identities.
Follow-up: How would you test object identity directly? Use the is operator when checking whether two references point to the same object.
id() have any observable interpreter behavior besides returning an integer?The function raises the builtins.id auditing event with the supplied object as its argument. This lets auditing mechanisms observe calls to the built-in.
Follow-up: What should code use for ordinary object identity checks? Prefer is; use id() when an integer identity representation is specifically needed.
isobjecttypehash