Skip to main content
🔀
PEP 622 · Speedrun · Python 3.10 (2021)

PEP 622 in 90 seconds — match / case isn't just a switch

Switch statements are a 5% feature. Pattern matching is the 95%.

What pattern matching actually does: ```python event = ("click", {"x": 10, "y": 20}) match event: case ("click", {"x": x, "y": y}): print(f"click at ({x}, {y})") ``` One `case` line: checks the **shape**, destructures `x` and `y`, and verifies the second element is a dict. All at once. That's not a switch — that's structural pattern matching.

C's `switch` checks equality (`case 1:`). Python's `match` checks **shape** (`case Point(x=0, y=y):`) or even "a list with one element that's a string". It's closer to Erlang or OCaml than to C. The 5% that overlaps with switch is the boring part. The 95% — destructure + type-check + bind in one line — is why people who saw it said "finally".

Before — nested if/elif checking shape
# Parse an event tuple
def handle(event):
    if isinstance(event, tuple) and len(event) == 2:
        kind, payload = event
        if kind == "click" and isinstance(payload, dict):
            return f"click at ({payload['x']}, {payload['y']})"
        elif kind == "key" and isinstance(payload, str):
            return f"key {payload}"
    return "unknown"
After — pattern matching (Python ≥3.10)
def handle(event):
    match event:
        case ("click", {"x": x, "y": y}):
            return f"click at ({x}, {y})"
        case ("key", str(k)):
            return f"key {k}"
        case _:
            return "unknown"

🎯 Predict the output

What does this print?

def describe(point):
    match point:
        case (0, 0):
            return "origin"
        case (0, y):
            return f"y-axis at {y}"
        case (x, 0):
            return f"x-axis at {x}"
        case (x, y):
            return f"point ({x}, {y})"

print(describe((0, 0)))
print(describe((0, 5)))
print(describe((3, 4)))
Deep dive into modern Python → Senior track

Or speedrun another PEP

PEP 572Assignment as an expression — 3 lines become 1.
PEP 572 in 90 seconds — the walrus that ate your for-loop
PEP 695Generic functions and classes, finally readable.
PEP 695 in 90 seconds — generic types without TypeVar
PEP 657From 'somewhere on this line' to 'this exact `[0]`'.
PEP 657 in 90 seconds — tracebacks that point at the actual sub-expression