Possible Duplicate:
defining “boolness” of a class in python
I thought this should print "False", why is it printing "True"?
>>> class Foo(object):
... def __bool__(self):
... return False
...
>>> f = Foo()
>>> if f:
... print "True"
... else:
... print "False"
...
True
>>>
You should define
__nonzero__()
in Python 2.x. It was only renamed to__bool__()
in Python 3.x. (The name__nonzero__()
actually predates the introduction of thebool
type by many years.)