For Python 2-3 compatibility, just add this to your example:
Foo.__nonzero__ = Foo.__bool__
or expand the original definition of Foo to include:
__nonzero__ = __bool__
You could of course define them in reverse too, where the method name is __nonzero__
and you assign it to __bool__
, but I think the name __nonzero__
is just a legacy of the original C-ishness of Python's interpretation of objects as truthy or falsy based on their equivalence with zero. Just add the statement above and your code will work with Python 2.x, and will automatically work when you upgrade to Python 3.x (and eventually you an drop the assignment to __nonzero__
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…