What is the difference between "a is b" and "id(a) == id(b)" in Python?
- by bp
The id() inbuilt function gives...
an integer (or long integer) which is guaranteed to be unique and constant for this object during its lifetime.
The is operator, instead, gives...
object identity
So why is it possible to have two objects that have the same id but return False to an is check? Here is an example:
>>> class Test():
... def test():
... pass
>>> a = Test()
>>> b = Test()
>>> id(a.test) == id(b.test)
True
>>> a.test is b.test
False
A more troubling example: (continuing the above)
>>> b = a
>>> b is a
True
>>> b.test is a.test
False
>>> a.test is a.test
False