Keywords and Control Flow
match — Structural pattern matching
Evaluate a subject and compare it with case patterns in source order. The first successful pattern whose optional guard is truthy selects the suite; no fallthrough occurs. Patterns can bind names and inspect structure instead of only comparing values. If no case qualifies, execution continues after the statement. Do not rely on partial bindings left by failed matches; their behavior is deliberately unspecified. A match statement requires at least one case.
Formal Syntax & Structure
python syntaxpython
match subject: case pattern [if guard]: suite ...Working Example
Example Snippetpython
message = ("move", 3, 5)match message: case ("move", x, y): print(x + y) case _: print("unknown")