Search code examples
pythonobjectlifetime

How can two Python objects have same id but 'is' operator returns False?


id(t+t), id(t*2)
(42838592, 42838592)

(t+t) is (t*2)
False

If two variable point to same object 'is' operator will return true.But first line said both have same id but 'is' operator give false value.


Solution

  • In the first example, your objects don't overlap in time: one is created then destroyed, then another is created with the same id.

    When you compare them with is, you are holding onto both objects, so they get different ids.