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