Nicholas Liu Nicholas Liu - 1 year ago 90
Python Question

In Python, is None a unique object?

In my test code, why the

results for tuple and None are different? It seems that a, b point to the same object
, but c, d point to different objects, though their values are the same (both equal to

I know that
keyword checks for the object lying behind rather than just compares value like
. And here I use tuple because I think tuple is immutable so that at least it's not a matter of whether the object is mutable or immutable.

But why variables a, b don't create and point to two different
objects as c, d do to tuple?

print(a is b)
print(c is d)

Answer Source

None is a singleton object: only one instance of it can ever exist. So objects referencing None will always have the same identity, and two or more of such objects will always pass the identity (object equality) test:

>>> a = None
>>> b = None
>>> c = None
>>> a is b is c

Same applies to True and False

>>> d = True
>>> (a is b is c) is d # proof of concept only
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download