> ## 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 Group Pattern

A group pattern in Python's structural pattern matching consists of a single sub-pattern enclosed in parentheses. It is used to explicitly define evaluation precedence within complex patterns, without altering the matching semantics of the enclosed sub-pattern itself.

A group pattern `(P)` succeeds if and only if the sub-pattern `P` succeeds. It does not enforce any type checking or structural constraints on the subject beyond what `P` already dictates.

## Syntax

```python theme={"dark"}
( <pattern> )
```

## Mechanics and Precedence

The primary function of a group pattern is to explicitly control the parsing associativity of pattern operators, particularly the OR pattern (`|`) and the AS pattern (`as`).

According to Python's pattern matching grammar, the `as` operator binds less tightly than the `|` operator. Consequently, an `as` pattern cannot be directly embedded as an alternative inside an OR pattern. Group patterns are grammatically required to isolate the `as` binding to individual alternatives.

```python theme={"dark"}
match subject:
    # Default precedence: The 'as' operator binds to the result of the entire OR pattern.
    case str() | int() as x:
        pass
        
    # Group patterns override precedence: The 'as' operator binds to each 
    # alternative individually before the OR pattern is evaluated.
    case (str() as x) | (int() as x):
        pass
```

## Disambiguation from Sequence Patterns

A critical technical distinction in Python's pattern matching grammar is the difference between a group pattern and a sequence pattern (which matches tuples). Parentheses alone do not denote a tuple in pattern matching; the presence of a comma dictates the sequence.

* **(P)**: Group pattern. Matches whatever `P` matches.
* **(P,)**: Sequence pattern. Matches a sequence (like a tuple or list) containing exactly one element that matches `P`.
* **()**: Sequence pattern. Matches an empty sequence.

```python theme={"dark"}
match subject:
    case (int()):
        # GROUP PATTERN
        # Matches the integer 42. Does NOT match the tuple (42,).
        pass
        
    case (int(),):
        # SEQUENCE PATTERN
        # Matches the tuple (42,). Does NOT match the integer 42.
        pass
```

## Variable Binding Constraints

Group patterns can be nested arbitrarily deep. However, when using group patterns to encapsulate alternatives within an OR pattern (`|`), Python's compiler strictly enforces that all alternatives bind the exact same set of variables.

Failing to bind the identical variable names across all branches of an OR pattern results in a compile-time `SyntaxError`. This prevents runtime scenarios where a variable might be unexpectedly unbound in the `case` block.

```python theme={"dark"}
match subject:
    # VALID: Both grouped alternatives bind the exact same variable name ('x').
    case (str() as x) | (int() as x):
        pass
        
    # INVALID: Raises SyntaxError: alternative patterns bind different names.
    # The left group binds 's', while the right group binds 'i'.
    # case (str() as s) | (int() as i):
    #     pass
```

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