DEV Community

Vivis Dev
Vivis Dev

Posted on • Originally published at pythonkoans.substack.com

Understanding truthiness, falsiness, and the quiet meaning of emptiness in Python

The Hidden Teaching: __bool__ and the Design of Truth

In Python, truth is not a binary of True and False. It is a reflection of meaning. Python asks, “In the context of logic, what does this object say about itself?”

To control how an object behaves in a boolean context, such as in an if statement, you may define the __bool__ method.

Let us begin again.

Part 1: The Interpreter’s Question

When Python evaluates an object in a boolean context like:

if some_object: 
    ...
Enter fullscreen mode Exit fullscreen mode

It performs this sequence:

  1. Call some_object.__bool__(), if defined.

  2. If not defined, fall back to some_object.__len__():

    • If length is 0, treat as False.
    • Otherwise, treat as True.
  3. If neither are defined, default to True

Thus, even a custom class can define its own measure of truth.

Part 2: Writing Our Own Truth

Let us construct a simple object: a container of wisdom.

class Scroll:
    def __init__(self, text):
        self.text = text
Enter fullscreen mode Exit fullscreen mode

Now, let us observe its truth:

s = Scroll("Do not seek the truth, only cease to cherish opinions.")

if s:
    print("The scroll speaks.")
else:
    print("The scroll is silent.")
Enter fullscreen mode Exit fullscreen mode

This will always print "The scroll speaks.", because Scroll has no __bool__ or __len__. Python defaults to treating all objects as True unless told otherwise.

Now, let us give it a voice of truth:

class Scroll:
    def __init__(self, text):
        self.text = text

    def __bool__(self):
        return bool(self.text.strip())
Enter fullscreen mode Exit fullscreen mode

Now, observe:

Scroll(" ") # Behaves like False
Scroll("Peace") # Behaves like True
Enter fullscreen mode Exit fullscreen mode

We have taught our object how to express its presence.

Part 3: Falling Back on __len__

If __bool__ is not defined, Python looks for __len__.

class WisdomBasket:
    def __init__(self):
        self.sayings = []

    def __len__(self):
        return len(self.sayings)
Enter fullscreen mode Exit fullscreen mode

Now:

basket = WisdomBasket()
if basket:
    print("There is wisdom.")
else:
    print("The basket is empty.")
Enter fullscreen mode Exit fullscreen mode

This is useful when your object represents a collection, and truth is tied to its contents.

Part 4: Truth as a Design Decision

Why does this matter?

Because in real-world code, truthiness is often meaning. Consider:

if response:
    ...
Enter fullscreen mode Exit fullscreen mode

What should response mean?

  • Did the HTTP request succeed?
  • Did the response body contain useful data?
  • Was the status 200?

You decide what __bool__ should convey.

When designing APIs, internal libraries, or user-defined classes, a clear definition of truth can reduce boilerplate and make intent self-evident.

Part 5: A Caution on Ambiguity

Truthiness should be unambiguous and intuitive.

If your object represents a configuration, a request, or a resource, consider what it means to be “truthy.” Avoid cleverness that makes the logic obscure:

if config:
    # What does this mean?
Enter fullscreen mode Exit fullscreen mode

Better to document, or avoid using truthiness altogether if the semantics are unclear.

Closing the Circle

The master did not say, "The empty list is False."
He said, "Not false. Only empty."

In Python, truth is not fixed. It is a shadow cast by your design. You may choose how your objects reflect light, or the absence of it.


If you enjoyed this post, consider subscribing or sharing it with your friends:

Python Koans | Vivis Dev | Substack

Python lessons wrapped in koans. Small puzzles, deep truths. Not your usual tutorial thread. Click to read Python Koans, by Vivis Dev, a Substack publication with hundreds of subscribers.

favicon pythonkoans.substack.com

Top comments (0)