vivis vivis avatar dev

> thoughts on AI, code, and everything in between

Koan 1: The Empty Path

, , ,

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: 
    ...

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.


If you're enjoying this post, consider subscribing for more posts like this:


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

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

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())

Now, observe:

Scroll(" ") # Behaves like False
Scroll("Peace") # Behaves like True

We have taught our object how to express its presence.


If you're enjoying this post, consider subscribing for more posts like this:


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)

Now:

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

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:
    ...

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?

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.

Thanks for reading Python Koans! If you enjoyed this post, share it with your friends: