Defining "Boolness" of a Class in Python

Defining boolness of a class in python

For Python 2-3 compatibility, add a line after the class definition block to alias the method:

class Foo(object):
...

Foo.__nonzero__ = Foo.__bool__

or include the alias directly in the class definition:

class Foo(object):
def __bool__(self):
...

__nonzero__ = __bool__

Of course this would also work the other way around, 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 the code will work with regardless of the version of Python (and the __nonzero__ definition can be dropped when support for 2.x is no longer needed).

How to define a false status for a class in Python

Add a __bool__ method to your class:

def __bool__(self):
return bool(self.name)

To cater for the case where self.name == '' and you want to return True:

def __bool__(self):
return self.name is not False

Assign class boolean value in Python

You need to implement the __nonzero__ method on your class. This should return True or False to determine the truth value:

class MyClass(object):
def __init__(self, val):
self.val = val
def __nonzero__(self):
return self.val != 0 #This is an example, you can use any condition

x = MyClass(0)
if not x:
print 'x is false'

If __nonzero__ has not been defined, the implementation will call __len__ and the instance will be considered True if it returned a nonzero value. If __len__ hasn't been defined either, all instances will be considered True.

In Python 3, __bool__ is used instead of __nonzero__.

overriding bool() for custom class

Is this Python 2.x or Python 3.x? For Python 2.x you are looking to override __nonzero__ instead.

class test:
def __nonzero__(self):
return False

Digging deeper into python if statements

Objects have __bool__ methods that are called when an object needs to be treated as a boolean value. You can see that with a simple test:

class Test:
def __bool__(self):
print("Bool called")
return False

t = Test()
if t: # Prints "Bool Called"
pass

bool(0) gives False, so 0 is considered to be a "falsey" value.

A class can also be considered to be truthy or falsey based on it's reported length as well:

class Test:
def __len__(self):
print("Len called")
return 0

t = Test()
if t:
pass

How to overload Python's __bool__ method in 2.x?

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 the bool type by many years.)

How to implement a if test in a customized class

If what you want is to use instances of your class as boolean values, i.e., some of the instances of your class are truthy and some are falsy, then, in your class, define __nonzero__ appropriately.

See Section 5.1 in the docs

By default, saying

if myobject:

where myobject is an instance of some class, will evaluate as if you wrote

if True:

Defining __nonzero__() allows you customize how your objects are used in a boolean context (so to speak).

For Python 3, things are a little different. See this SO question.



Related Topics



Leave a reply



Submit