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

The `as` pattern in Python's structural pattern matching (PEP 634) binds a successfully matched pattern or sub-pattern to a local identifier. It evaluates the structural condition on the left side of the `as` keyword and, upon a successful match, assigns the corresponding subject value to the variable name on the right side.

**Syntax**

```python theme={"dark"}
case <pattern> as <identifier>:
```

**Evaluation Mechanics**

1. The interpreter evaluates the `<pattern>` against the subject node.
2. If the `<pattern>` fails to match, the `as` pattern fails, and no binding occurs.
3. If the `<pattern>` succeeds, the `as` pattern succeeds, and the matched subject is bound to the `<identifier>`.
4. The `<identifier>` is injected into the surrounding local scope (e.g., the function or module scope). Python does not enforce block-level scope for `match`/`case` statements, meaning the bound variable outlives the `match` block and remains accessible in the broader namespace.

**Structural Implementations**

**1. Top-Level Binding**
When applied at the root of a `case`, the `as` pattern captures the entire subject, provided the left-hand pattern evaluates to true.

```python theme={"dark"}
match subject:
    case list() as full_list:
        pass
```

**2. Sub-Pattern Binding (Nesting)**
The `as` pattern evaluates strictly at the node level where it is defined. When nested inside sequences, mappings, or class patterns, it captures only the specific element that matched the sub-pattern, not the entire subject.

```python theme={"dark"}
match subject:
    case [1, (int() as middle_val), 3]:
        pass
```

**3. OR Pattern Capture**
The `as` pattern is frequently combined with the OR pattern (`|`). Because an OR pattern does not inherently bind the matched alternative to a variable, appending `as` captures whichever structural alternative successfully evaluated.

```python theme={"dark"}
match subject:
    case {"status": 200} | {"status": 201} as success_payload:
        pass
```

**Technical Constraints and Precedence**

* **Operator Precedence:** The `as` pattern binds looser than the OR operator (`|`) but tighter than the open sequence operator (`,`).
  * In the expression `case pattern1 | pattern2 as name`, the OR pattern is evaluated first, meaning the entire OR expression is bound to `name` as `(pattern1 | pattern2) as name`.
  * In the expression `case pattern1, pattern2 as name`, the `as` pattern binds only to the second element, evaluated as `pattern1, (pattern2 as name)`. To bind the entire sequence, explicit grouping is required: `case (pattern1, pattern2) as name`.
* **Name Collisions:** Python enforces strict uniqueness for bindings within a single sequence or composite pattern. You cannot bind the same identifier multiple times within a single pattern structure. Attempting to do so raises a `SyntaxError`.
  ```python theme={"dark"}
  ```

match subject:

# Raises SyntaxError: multiple assignments to name 'x' in pattern

case \[int() as x, float() as x]:
pass

````
* **OR Pattern Binding Constraints:** If an identifier is bound in one alternative of an OR (`|`) pattern, it **must** be bound in all other alternatives of that exact same OR pattern. Python strictly prohibits binding a name in only a subset of an OR pattern's alternatives.
  ```python
match subject:
    # Raises SyntaxError: alternative patterns bind different names
    case {"a": x} | {"b": y}:
        pass
        
    # Raises SyntaxError: alternative patterns bind different names
    # 'name' is bound in the right alternative but not the left
    case pattern1 | (pattern2 as name):
        pass
        
    # Valid: 'x' is bound in all alternatives
    case {"a": x} | {"b": x}:
        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>
