Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The OR pattern is a structural pattern matching construct in Python (introduced in version 3.10 via PEP 634) that allows a single case block to match a subject value against multiple alternative sub-patterns. It is denoted by the pipe character (|) and evaluates alternatives sequentially, succeeding if any of the specified sub-patterns successfully match the subject.

Syntax

match subject:
    case pattern_a | pattern_b | pattern_c:
        # Executes if subject matches pattern_a, pattern_b, OR pattern_c
        pass

Execution Mechanics

  1. Left-to-Right Evaluation: The Python interpreter evaluates the sub-patterns separated by | strictly from left to right.
  2. Short-Circuiting: Evaluation stops immediately upon the first successful match. Subsequent sub-patterns in the same case statement are not evaluated.
  3. Sub-pattern Composition: The OR pattern can combine literal patterns, value patterns, sequence patterns, mapping patterns, or class patterns.

Name Binding Rules

When using capture patterns (variable binding) within an OR pattern, Python enforces strict structural equivalence regarding the bound names. Every alternative within the OR pattern must bind the exact same set of variables. If the alternatives bind different variables, or if one alternative binds a variable while another does not, the interpreter raises a SyntaxError at compile time. Valid Binding:
match data:
    # Both sub-patterns bind the variable 'x'
    case [1, x] | [2, x]:
        pass
Invalid Binding (SyntaxError):
match data:
    # Fails: The first pattern binds 'x', the second binds 'y'
    case [1, x] | [2, y]: 
        pass

match data:
    # Fails: The first pattern binds 'x', the second binds nothing
    case [1, x] | [2, 3]: 
        pass

Wildcards in OR Patterns

Python explicitly forbids using the wildcard pattern (_) as an alternative within an OR pattern. Attempting to include a wildcard as a sub-pattern in an OR pattern will raise a SyntaxError. Invalid Wildcard Usage (SyntaxError):
match subject:
    # Fails: Wildcards are not allowed as sub-patterns in an OR pattern
    case 1 | 2 | _:
        pass

Nesting OR Patterns

The OR pattern does not have to exist at the top level of the case statement. It can be nested deeply within sequences, mappings, or class patterns to create highly specific structural constraints. Nested within a Sequence:
match sequence:
    case [0, 1 | 2 | 3, 4]:
        # Matches [0, 1, 4], [0, 2, 4], or [0, 3, 4]
        pass
Nested within a Mapping:
match payload:
    case {"type": "auth" | "ping", "id": session_id}:
        # Matches a dictionary where the "type" key is exactly "auth" or "ping"
        pass
Nested within a Class Pattern:
match node:
    case ASTNode(type="Literal" | "Identifier", value=val):
        # Matches an ASTNode instance with specific attribute values
        pass

Precedence and the AS Pattern

In Python’s pattern matching grammar, the OR pattern (|) has higher precedence than the AS pattern (as). Consequently, an as binding at the end of an OR pattern applies to the entire OR pattern, not just the immediately preceding sub-pattern. Parentheses are not required to bind the result of the entire OR pattern. Valid Binding:
match subject:
    # Parses as: case (1 | 2 | 3) as val:
    # The OR pattern is evaluated first, and the successful match is bound to 'val'.
    case 1 | 2 | 3 as val:
        print(val)
Invalid Binding (SyntaxError):
match subject:
    # Forces the 'as' binding only on the last alternative.
    # Fails: Violates name binding rules because the third alternative binds 'val', 
    # but the first and second alternatives do not.
    case 1 | 2 | (3 as val):
        print(val)
Master Python with Deep Grasping Methodology!Learn More