beps/docs/proposals/BEP-002-match/context/python.md
Introduced in Python 3.10 (PEP 634), the match statement provides structural pattern matching.
match command:
case "quit":
quit()
case "reset":
reset()
case _:
print("Unknown command")
Matches against the structure of data (sequences, mappings, objects).
match point:
case (0, 0):
print("Origin")
case (0, y):
print(f"Y={y}")
case (x, 0):
print(f"X={x}")
case (x, y):
print(f"X={x}, Y={y}")
Matches against class attributes.
@dataclass
class Point:
x: int
y: int
match point:
case Point(x=0, y=0):
print("Origin")
case Point(x=0, y=y):
print(f"Y={y}")
if clauses can be added to cases.
match point:
case Point(x, y) if x == y:
print(f"Y=X at {x}")
case Point(x, y):
print(f"Not on diagonal")
Variables can capture parts of the match.
match command.split():
case ["go", direction]:
print(f"Going {direction}")
case ["drop", *items]:
print(f"Dropping {items}")
Combine patterns with |.
match status:
case 401 | 403 | 404:
print("Not allowed")
case 200:
print("OK")
Python's match is a statement, not an expression. It does not return a value.