__missing__Easy Examples

Called by dict subclasses when a key is not found

Implementing __missing__

Basic implementation of __missing__ in a class.

python
class Example:
    def __missing__(self):
        return "Example __missing__"

obj = Example()
print(obj)

__missing__ called by dict subclasses when a key is not found. Implementing it lets you customize how Python interacts with your objects.

__missing__ in action

Seeing __missing__ called by Python's built-in operations.

python
# How Python calls __missing__ automatically
class Demo:
    def __init__(self, value):
        self.value = value

    def __missing__(self):
        print(f"__missing__ was called!")
        return self

d = Demo(42)
# This triggers __missing__:
print(d)

Python automatically calls __missing__ when you use the corresponding operator or function on your object.

Want to try these examples interactively?

Open Easy Playground