Search GimmeJob

Type at least 2 characters. Enter also searches by meaning.
Python interview question
Core language & syntaxMiddleCommonTheory

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

Python 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.

#pattern-matching#pep-634

Sources

PEP 634 — Structural Pattern Matching: SpecificationPython Software Foundation · match/case semantics introduced in Python 3.10What's new in Python 3.14Python Software Foundation · Current-version language and runtime changes, including template strings and free-threading support