🐍 Python Intermediate

What is Python's structural pattern matching (match/case)?

Why Interviewers Ask This

This tests whether you can apply Python knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.

Answer

Structural pattern matching (Python 3.10, PEP 634) adds a match/case statement — more powerful than a simple switch. Example: match command: case "quit": quit(); case "go" if direction: go(direction); case {"action": action, "data": data}: handle(action, data); case _: print("unknown"). Patterns: literal (value equality), capture (bind to variable), class (case Point(x=0, y=y)), sequence (case [first, *rest]), mapping (case {"key": value}), OR (case 400 | 404 | 405), guard (case x if x > 0). The _ is the wildcard (does not bind). Match is an expression of structural pattern matching, not just value switching — it inspects the structure and type of the subject.

Pro Tip

This topic has Python-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.