In Python < 3.0 :
You have to use __nonzero__
to achieve what you want. It’s a method that is called automatically by Python when evaluating an object in a boolean context. It must return a boolean that will be used as the value to evaluate.
E.G :
class Foo(object):
def __init__(self, bar) :
self.bar = bar
def __nonzero__(self) :
return self.bar % 2 == 0
if __name__ == "__main__":
if (Foo(2)) : print "yess !"
In Python => 3.0 :
Same thing, except the method has been renamed to the much more obvious __bool__
.