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

# Python Or Pattern

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

```python theme={"dark"}
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:**

```python theme={"dark"}
match data:
    # Both sub-patterns bind the variable 'x'
    case [1, x] | [2, x]:
        pass
```

**Invalid Binding (`SyntaxError`):**

```python theme={"dark"}
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`):**

```python theme={"dark"}
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:**

```python theme={"dark"}
match sequence:
    case [0, 1 | 2 | 3, 4]:
        # Matches [0, 1, 4], [0, 2, 4], or [0, 3, 4]
        pass
```

**Nested within a Mapping:**

```python theme={"dark"}
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:**

```python theme={"dark"}
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:**

```python theme={"dark"}
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`):**

```python theme={"dark"}
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)
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
