Skip to main content

Command Palette

Search for a command to run...

Python: Truthiness of Objects

Tell me the Truth!

Published
2 min read
Python: Truthiness of Objects
M

I love teaching & accelerating people's learning. My goal is to unveil complex topics & leave my readers feeling enabled.

I think many members of the developer community act as "gatekeepers" to knowledge. This discourages people from improving themselves at times when they need encouragement the most. I hope to decrease this feeling & encourage people that they are capable of learning new & challenging concepts.

I look forward to sharing my knowledge & I hope what I share helps :)

Introduction:

This post is a brief overview of how Python evaluates the truthiness of objects. In Python, any object can be tested for truth value!

Built-in Objects:

The following built-in objects evaluate to False:

  • constants defined to be false: None and False.

  • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)

  • empty sequences and collections: '', (), [], {}, set(), range(0)

If a built-in object is not one of these values, it will evaluate to True. Below are examples of what some built-in objects evaluate to (the bool() method is a built-in method used to evaluate the truth value of objects):

bool(0)
False

bool(5)
True

bool([])
False

bool('hello')
True

bool(None)
False

Non Built-in Objects:

Non built-in objects are evaluated to True by default.

class Book:

    def __init__(self, pages: int) -> None:
        self.pages = pages

book_1 = Book(100)

bool(book_1)
True

To override this default behavior, the __bool__ method can be defined within a class. Within this dunder method, conditions can be set for when an object evaluates to True or False.

class Book:

    def __init__(self, pages: int) -> None:
        self.pages = pages

    def __bool__(self) -> bool:
        if self.pages < 0:
            return False
        return True

book_1 = Book(100)
book_2 = Book(-100)

bool(book_1)
True

bool(book_2)
False

If the __bool__ method is not defined, Python will then look for the __len__ method within the class. If the __len__ method is defined, then an object will evaluate to False if the __len__ method would return a value of 0 for that object.

class Book:

    def __init__(self, pages: int) -> None:
        self.pages = pages

    def __len__(self) -> int:
        return self.pages

book_1 = Book(100)
book_2 = Book(0)

bool(book_1)
True

bool(book_2)
False

In summary, the __bool__ method will take precedence over the __len__ method if both are defined in a class. If neither the __bool__ nor __len__ method are defined, all objects of the class will evaluate to True.

Functions & Methods as Objects:

Functions & methods, as objects, will always evaluate True.

class Book:

    def __init__(self, author: str, available: bool, pages: int) -> None:
        self.available = available
        self.author = author
        self.pages = pages

    def set_to_unavailable(self) -> None:
        self.available = False

def indepedent_function() -> None:
    print('I am a function.')

book_1 = Book('Mitch', True, 100)

bool(book_1.available)
True

bool(indepedent_function)
True

Hopefully this article has clarified how Python evaluates the truthiness of objects!