Skip to content

Today I Learned 1 min read

TIL - Python's match Statements Are More Powerful Than You Think

match statement introduced in Python 3.10 isn’t just a fancy switch. It supports structural pattern matching — matching against object types, destructuring, and guards.

def process(command):
    match command:
        case {"action": "move", "direction": str(d)}:
            print(f"Moving {d}")
        case {"action": "quit"}:
            print("Quitting")
        case _:
            print("Unknown command")
python

This is especially powerful for processing JSON/dict payloads in API handlers.