> ## 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 Nonlocal Variable

The `nonlocal` keyword in Python is used within nested functions to declare that a variable refers to a previously bound variable in the nearest enclosing lexical scope, excluding the global scope. It instructs the Python compiler to bypass the local namespace during variable assignment and instead bind the identifier to an existing variable in a parent function's namespace.

## Syntax

```python theme={"dark"}
nonlocal variable_name
```

Multiple variables can be declared on a single line by separating them with commas:

```python theme={"dark"}
nonlocal var1, var2, var3
```

## Scope Resolution and Rebinding

In Python's LEGB (Local, Enclosing, Global, Built-in) scope resolution, reading a variable automatically searches up the scope chain. However, assigning a value to a variable defaults to creating or updating a variable in the **Local** scope.

Without `nonlocal`, an assignment operation inside a nested function creates a new local variable that shadows the variable in the enclosing scope. The `nonlocal` statement overrides this default behavior, granting write access (rebinding capability) to the **Enclosing** scope.

### Mechanical Example

```python theme={"dark"}
def outer_function():
    x = "enclosing_value"
    
    def inner_function():
        nonlocal x
        # This rebinds the 'x' from outer_function, 
        # rather than creating a new local 'x'
        x = "mutated_value" 
        
    inner_function()
    print(x) 

outer_function() 

# Output: mutated_value
```

## Technical Constraints

1. **Compile-Time Binding Requirement:** The variable declared as `nonlocal` must be bound in an enclosing function's local scope. Because Python determines local variables for the entire function block at compile time, the `nonlocal` declaration is resolved during compilation, not at runtime. You cannot use `nonlocal` to dynamically create a new variable in a parent scope. However, because it is a compile-time declaration, the variable's assignment in the enclosing scope can textually appear *after* the nested function definition, provided it is bound within that enclosing function's local scope.

```python theme={"dark"}
def outer():
    # 'y' is never bound anywhere in outer's scope
    def inner():
        nonlocal y  # Raises SyntaxError: no binding for nonlocal 'y' found
        y = 1
```

2. **Exclusion of Global Scope:** `nonlocal` strictly searches enclosing function scopes. It will not resolve to variables defined in the global module scope. If the compiler exhausts all enclosing function scopes and hits the global scope without finding the variable, it raises a `SyntaxError`. To modify global variables, the `global` keyword must be used instead.
3. **Exclusion of Local Scope:** A variable cannot be declared as `nonlocal` if it has already been treated as a local variable in the current block. The `nonlocal` declaration must precede any assignment to that variable within the nested function.

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