I recently read somewhere that the special value None
in python is a singleton object of its own class, specifically NoneType
. This explained a lot
The NoneType overrides __new__
which always return the same singleton. The code is actually written in C so dis
cannot help, but conceptually it's just like this.
Having only one None instance is easier to deal with. They are all equal anyway.
By overriding __new__
... e.g.
class MyNoneType(object):
_common_none = 0
def __new__(cls):
return cls._common_none
MyNoneType._common_none = object.__new__(MyNoneType)
m1 = MyNoneType()
m2 = MyNoneType()
print(m1 is m2)