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

A capture pattern is a structural pattern matching construct in Python that binds a matched subject, or a specific sub-component of that subject, to a variable name. When a capture pattern succeeds, it assigns the matched value to the specified bare identifier, making that value accessible within the enclosing scope of the `match` statement.

## Syntax

A capture pattern can be used as a standalone identifier or combined with other patterns using the `as` keyword.

**Standalone Capture:**

```python theme={"dark"}
match subject:
    case identifier:
        pass
```

**Sub-pattern Capture (using `as`):**

```python theme={"dark"}
match subject:
    case [1, 2] as identifier:
        pass
```

## Technical Mechanics

**Bare Identifiers vs. Value Patterns**
A fundamental rule of Python pattern matching is the distinction between bare names and dotted names. A bare identifier (e.g., `case CONSTANT:`) is always treated as a capture pattern; it will capture the subject and overwrite (rebind) any existing variable with that name in the local scope. Because `match` blocks do not create a new local scope, the original value is permanently replaced. To compare a subject against an existing value without capturing it, a dotted name (e.g., `case config.CONSTANT:`) must be used. Dotted names are evaluated as *value patterns*, not capture patterns.

```python theme={"dark"}
match subject:
    case config.CONSTANT:
        # Compares the subject to the value of config.CONSTANT; does not capture
        pass
    case CONSTANT:
        # Captures the subject, overwriting any existing 'CONSTANT' variable
        pass
```

**Success, Irrefutability, and Guards**
Patterns in Python do not evaluate to boolean values; they either succeed or fail to match. A standalone capture pattern always succeeds. Because it acts as an unconditional catch-all, a bare capture pattern without a guard is an *irrefutable* pattern. The Python interpreter enforces that an irrefutable `case` block must be the final clause in a `match` statement; placing any `case` clauses after it results in a `SyntaxError`.

However, if the `case` clause includes a guard, the block becomes refutable. A refutable capture pattern can legally be followed by other `case` clauses.

```python theme={"dark"}
match subject:
    case x if x > 0:
        # Refutable due to the guard; subsequent cases are allowed
        pass
    case x:
        # Irrefutable; must be the final case clause
        pass
```

**Binding Restrictions**
A capture pattern cannot bind the same variable name more than once within a single pattern. Attempting to capture multiple elements into the same identifier raises a `SyntaxError`.

```python theme={"dark"}
match subject:
    case [x, y]:
        # Valid: binds two distinct identifiers
        pass
    # case [x, x]: 
        # Invalid: raises SyntaxError (multiple assignments to name 'x')
```

**Capture Patterns in OR Patterns (`|`)**
When capture patterns are used within an OR pattern, Python requires that every alternative in the OR pattern binds the exact same set of variable names. If an identifier is captured in one alternative, it must be captured in all of them.

```python theme={"dark"}
match subject:
    case [1, x] | [x, 2]:
        # Valid: 'x' is bound regardless of which alternative succeeds
        pass
    # case 1 | x:
        # Invalid: raises SyntaxError ('x' is not bound if the subject is 1)
```

**The `as` Keyword**
When a pattern requires type checking or specific structural validation before capturing, the `as` keyword binds the successfully validated node to a name. This enforces a structural match while simultaneously capturing the exact value that satisfied the sub-pattern.

```python theme={"dark"}
match subject:
    case [str(), int() as status_code]:
        # 'status_code' captures the second element only if it is an integer 
        # and the first element is a string.
        pass
```

**The Wildcard Exception**
The underscore `_` is a special identifier in structural pattern matching. While it always succeeds like a standalone capture pattern, it is technically a *wildcard pattern*. It matches any subject but explicitly **does not bind** the value to the name `_`.

**Scoping Rules and Guard Evaluation**
Variables bound via capture patterns do not have block-level scope. Following standard Python scoping rules, an identifier bound in a `case` clause leaks into the enclosing scope of the `match` statement.

The Python interpreter handles bindings internally during structural evaluation. If a pattern fails structurally at any point, no variables from that pattern are bound to the local scope. The entire structural pattern must succeed for the bindings to be committed.

However, variable binding occurs *before* a guard (`if` condition) is evaluated. If a pattern successfully matches structurally and binds variables, but the guard evaluates to `False`, the `case` block will not execute, yet the bound variables are still initialized and leak into the enclosing scope.

```python theme={"dark"}
subject = [1, 2]
match subject:
    case [x, y] if x > 5:
        pass
    case _:
        pass


# x is now bound to 1 and y is bound to 2 in the enclosing scope.

# The structural match [x, y] succeeded, committing the bindings, 

# even though the guard (x > 5) subsequently failed.
```

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