Skip to the content.
Pattern Matching in Python: A Modern Approach to Logic | AI Systems Design From Scratch

Connect with Amin Boulouma Official

🏠 Documentation Hub 📝 Engineering Blog 💻 GitHub Repository

Mastering Python’s match-case (Structural Pattern Matching)

Amin Boulouma, Software Engineer

Introduced in Python 3.10, the match-case statement is far more than a simple “switch-case” replacement found in other languages. It is a powerful Structural Pattern Matching engine that allows you to inspect the shape, type, and content of data with unparalleled clarity.

The Problem: The if-elif Bottleneck

Before match-case, handling different types of input often required tedious type-checking, length validation, and nested if statements. This led to “brittle” code that was difficult to extend.

The Solution: Structural Pattern Matching

match-case doesn’t just compare values; it matches patterns. It can decompose objects, extract variables, and enforce structure simultaneously.

1. Basic Value Matching

Think of this as the modern, safer alternative to if-elif chains.

def get_status_message(status_code):
    match status_code:
        case 200:
            return "OK"
        case 404:
            return "Not Found"
        case 500:
            return "Server Error"
        case _:  # The wildcard (default) case
            return "Unknown Status"

2. Sequence and Pattern Matching

This is where match-case shines. You can match the structure of lists or tuples and extract values in one step.

def handle_command(command):
    match command.split():
        case ["quit"]:
            quit_app()
        case ["load", filename]:  # Extracts the filename directly
            load_file(filename)
        case ["move", x, y] if int(x) < 0:  # With a guard condition
            print("Move to negative coordinate invalid")
        case ["move", x, y]:
            move_object(int(x), int(y))
        case _:
            print("Invalid command")

Advanced Capabilities

  1. Guards: Use if inside a case to add conditional logic to your patterns.
  2. Or Patterns: Match multiple values using the pipe | operator (e.g., case 401 | 403 | 404:).
  3. Class Patterns: You can match against specific object types and capture their attributes effortlessly.
match event:
    case Click(x, y) if x > 100:
        print(f"Clicked on the right side: {x}, {y}")
    case KeyPress(key):
        print(f"Key pressed: {key}")

Comparison: if-elif vs match-case

Feature if-elif Chains match-case
Data Inspection Manual (requires isinstance()) Automatic (structural matching)
Readability Becomes cluttered with depth Clean, declarative syntax
Logic Extraction Manual assignment Direct variable capture
Complexity High (Cyclomatic complexity) Low (Optimized for patterns)

Best Practices

By adopting match-case, you move away from imperative “check-and-branch” programming toward a declarative style that describes what your data should look like, making your code significantly more resilient and easier to maintain.

Connect with Amin Boulouma Official