pythonidentityidentity-operator

id() vs `is` operator. Is it safe to compare `id`s? Does the same `id` mean the same object?


How much can I rely on the object's id() and its uniqueness in practice? E.g.:

(Written as a proposed canonical in response to Canonicals for Python: are objects with the same id() the same object, `is` operator, unbound method objects)


Solution

  • According to the id() documentation, an id is only guaranteed to be unique

    1. for the lifetime of the specific object, and
    2. within a specific interpreter instance

    As such, comparing ids is not safe unless you also somehow ensure that both objects whose ids are taken are still alive at the time of comparison (and are associated with the same Python interpreter instance, but you need to really try to make that become false).

    Which is exactly what is does -- which makes comparing ids redundant. If you cannot use the is syntax for whatever reason, there's always operator.is_.


    Now, whether an object is still alive at the time of comparison is not always obvious (and sometimes is grossly non-obvious):


    Due to the above safety requirements when comparing ids, saving an id instead of the object is not very useful because you have to save a reference to the object itself anyway -- to ensure that it stays alive. Neither is there any performance gain: is implementation is as simple as comparing pointers.


    Finally, as an internal optimization (and implementation detail, so this may differ between implementations and releases), CPython reuses some often-used simple objects of immutable types. As of this writing, that includes small integers and some strings. So even if you got them from different places, their ids might coincide.

    This does not (technically) violate the above id() documentation's uniqueness promises: the reused object stays alive through all the reuses.

    This is also not a big deal because whether two variables point to the same object or not is only practical to know if the object is mutable: if two variables point to the same mutable object, mutating one will (unexpectedly) change the other, too. Immutable types don't have that problem, so for them, it doesn't matter if two variables point to two identical objects or to the same one.


    1Sometimes, this is called "unnamed expression".