In the first test, Python try to convert the object to a bool
value if it is not already one. Roughly, we are asking the object : are you meaningful or not ? This is done using the following algorithm :
If the object has a __nonzero__
special method (as do numeric built-ins, int
and float
), it calls this method. It must either return a bool
value which is then directly used, or an int
value that is considered False
if equal to zero.
Otherwise, if the object has a __len__
special method (as do container built-ins, list
, dict
, set
, tuple
, ...), it calls this method, considering a container False
if it is empty (length is zero).
Otherwise, the object is considered True
unless it is None
in which case, it is considered False
.
In the second test, the object is compared for equality to None
. Here, we are asking the object, "Are you equal to this other value?" This is done using the following algorithm :
If the object has a __eq__
method, it is called, and the return value is then converted to a bool
value and used to determine the outcome of the if
.
Otherwise, if the object has a __cmp__
method, it is called. This function must return an int
indicating the order of the two object (-1
if self < other
, 0
if self == other
, +1
if self > other
).
Otherwise, the object are compared for identity (ie. they are reference to the same object, as can be tested by the is
operator).
There is another test possible using the is
operator. We would be asking the object, "Are you this particular object?"
Generally, I would recommend to use the first test with non-numerical values, to use the test for equality when you want to compare objects of the same nature (two strings, two numbers, ...) and to check for identity only when using sentinel values (None
meaning not initialized for a member field for exemple, or when using the getattr
or the __getitem__
methods).
To summarize, we have :
>>> class A(object):
... def __repr__(self):
... return 'A()'
... def __nonzero__(self):
... return False
>>> class B(object):
... def __repr__(self):
... return 'B()'
... def __len__(self):
... return 0
>>> class C(object):
... def __repr__(self):
... return 'C()'
... def __cmp__(self, other):
... return 0
>>> class D(object):
... def __repr__(self):
... return 'D()'
... def __eq__(self, other):
... return True
>>> for obj in ['', (), [], {}, 0, 0., A(), B(), C(), D(), None]:
... print '%4s: bool(obj) -> %5s, obj == None -> %5s, obj is None -> %5s' %
... (repr(obj), bool(obj), obj == None, obj is None)
'': bool(obj) -> False, obj == None -> False, obj is None -> False
(): bool(obj) -> False, obj == None -> False, obj is None -> False
[]: bool(obj) -> False, obj == None -> False, obj is None -> False
{}: bool(obj) -> False, obj == None -> False, obj is None -> False
0: bool(obj) -> False, obj == None -> False, obj is None -> False
0.0: bool(obj) -> False, obj == None -> False, obj is None -> False
A(): bool(obj) -> False, obj == None -> False, obj is None -> False
B(): bool(obj) -> False, obj == None -> False, obj is None -> False
C(): bool(obj) -> True, obj == None -> True, obj is None -> False
D(): bool(obj) -> True, obj == None -> True, obj is None -> False
None: bool(obj) -> False, obj == None -> True, obj is None -> True