How does Python's `match`/`case` structural pattern matching differ from a C-style switch statement?
Answer
Introduced in Python 3.10 by PEP 634, `match`/`case` matches the shape and structure of a value, not just its raw equality to a constant. Patterns can destructure sequences, mappings and class instances, bind matched sub-values to names, add `if` guard clauses, and combine alternatives with `|`. A `case _:` acts as the wildcard default, and unlike a C switch there is no implicit fall-through between cases.
Strong answer includes
- structural, not just equality, matching
- introduced by PEP 634 in 3.10
- no fall-through, wildcard is case _
Practical example
match command.split():
case ["go", direction]: move(direction)
case ["look"]: describe_room()
case _: print("unknown command")destructures the split command list directly in the pattern.