what exactly is "None" ?

Christopher A. Craig list-python at ccraig.org
Tue Mar 4 19:22:56 CET 2003

"Andrei Doicin" <Andrei.Doicin at cern.ch> writes:

> I know that "None" is the Boolean false value that
> gets thrown out when there's nothing to return or a
> function doesn't manage to reach the end of itself and thus "return
> whatever", but *what* (in Python terms) exactly is equal to "None"
> ???

The short answer: None and no other builtin type is equal to None, so
you're probably safe.  If you want to be really safe, use 'is' instead
of '=='.  The 'is' operator returns true if and only if the two
objects have the same object_id number (i.e. occupy the same space in
memory), so if will only ever work for objects that are exactly the

The long answer: 

Since you asked what "exactly" compares true, here's the full list.

None does not implement comparison, so comparison with None falls to
either the other object's compare function or to the comparison of
object_id numbers (which will only compare equal if both objects are
the None object).  Therefore:

Any object which defines self.__eq__() such that 'self.__eq__(None)' is
true is equal to None.

Any object for which self.__eq__() is undefined and self.__cmp__() is
defined such that 'self.__cmp__(None) == 0' is equal to None.

There are no builtin types (besides None) for which the above is true,
so you would have to define your own type to do this, which you can do

>>> class equalseverything:
...  def __eq__(self, other): return 1
>>> t = equalseverything()
>>> t == None
>>> t is None

In Python2.2 and earlier (i.e. all current production versions) None
is mutable, so you could assign 6 to None and then anything equal to 6
would return equal.  Merely assigning to None doesn't change what is
returned by functions without a return statement though, that's still
the real None object.  I've never seen anyone actually do this (thank

>>> None = 6
>>> None == 6.0
>>> def t(): pass
>>> a = t()
>>> a
>>> a == None
>>> a is None
>>> del None
>>> a is None

Christopher A. Craig <list-python at ccraig.org>
"That's so obvious that someone has already 
got a patent on it."  -- Guido van Rossum

More information about the Python-list mailing list