Defining "boolness" of a class in python

PythonPython 2.7ClassBooleanPython 2.x

Python Problem Overview


Why doesn't this work as one may have naively expected?

class Foo(object):
    def __init__(self):
        self.bar = 3
    def __bool__(self):
        return self.bar > 10

foo = Foo()

if foo:
    print 'x'
else:
    print 'y'

(The output is x)

Python Solutions


Solution 1 - Python

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__).

Solution 2 - Python

The __bool__ method is used in Python 3. For Python 2, you want __nonzero__.

Solution 3 - Python

Because the corresponding special method is called __nonzero__() in Python 2, and not __bool__() until Python 3.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwimView Question on Stackoverflow
Solution 1 - PythonPaulMcGView Answer on Stackoverflow
Solution 2 - PythonCat Plus PlusView Answer on Stackoverflow
Solution 3 - PythonSven MarnachView Answer on Stackoverflow