36
可能重複:
defining 「boolness」 of a class in python如何重載Python的__bool__方法?
我想這應該打印 「假」,爲什麼它打印 「真」?
>>> class Foo(object):
... def __bool__(self):
... return False
...
>>> f = Foo()
>>> if f:
... print "True"
... else:
... print "False"
...
True
>>>
Dup of [overriding bool()for custom class](http://stackoverflow.com/questions/2233786) – outis 2012-01-19 11:08:07