# Closures, LEGB Rule, and nonlocal

> Explain Python closures and the LEGB scoping rule. What does nonlocal do?

Canonical: https://www.internhack.in/learn/interview/python-interview/python-closures-scope
Last updated: 2026-11-28

Difficulty: Intermediate

## Question

Explain Python closures and the LEGB scoping rule. What does nonlocal do?

## Answer

LEGB rule - Python looks up names in this order:

1. Local: current function scope
2. Enclosing: enclosing function scopes (for nested functions)
3. Global: module-level scope
4. Built-in: Python built-in names (len, print, etc.)

Closure: a nested function that captures free variables from its enclosing scope. The captured variables are stored in the closure's \__closure_\_ attribute.

nonlocal: declares that a name refers to the enclosing scope's variable and allows assignment to it (without nonlocal, assignment creates a new local variable).

## Code examples

### LEGB and Closures

```
x = 'global'

def outer():
    x = 'enclosing'

    def inner():
        # x = 'local'  # Would shadow enclosing x
        print(x)  # Finds x in enclosing scope (LEGB)

    inner()

outer()  # enclosing

# Closure - counter factory
def make_counter(start=0):
    count = start  # Free variable captured by closure

    def increment(step=1):
        nonlocal count  # Modify enclosing count
        count += step
        return count

    def reset():
        nonlocal count
        count = start

    def get():
        return count

    return increment, reset, get

inc, reset, get = make_counter(10)
print(inc())    # 11
print(inc(5))   # 16
print(get())    # 16
reset()
print(get())    # 10

# Inspect closure
print(inc.__code__.co_freevars)  # ('count', 'start')
print(inc.__closure__[0].cell_contents)  # 10
```

nonlocal allows modifying the enclosing scope's variable. Without it, count += step would create a new local count (UnboundLocalError).

### Classic Closure Gotcha and Fix

```
# Classic gotcha: loop variable in closure
functions = []
for i in range(5):
    functions.append(lambda: i)  # All capture same i

result = [f() for f in functions]
print(result)  # [4, 4, 4, 4, 4] - i is 4 at call time

# Fix 1: default argument captures current value
functions = []
for i in range(5):
    functions.append(lambda i=i: i)  # i=i captures current i

print([f() for f in functions])  # [0, 1, 2, 3, 4]

# Fix 2: factory function
def make_fn(i):
    return lambda: i

functions = [make_fn(i) for i in range(5)]
print([f() for f in functions])  # [0, 1, 2, 3, 4]

# global keyword
total = 0

def add_to_total(n):
    global total  # Without this, assignment creates local total
    total += n

add_to_total(5)
add_to_total(3)
print(total)  # 8
```

The loop gotcha: closures capture the variable, not its value. The lambda captures the reference to i, which is 4 when all functions are called.

## Key points

- Assignment in a function creates a local variable - that is why nonlocal/global are needed
- Closures are commonly used for decorators, callbacks, and factory functions
- A function is a closure if it has \__closure_\_ is not None
- Reading a variable (without assignment) uses LEGB; assigning creates a local

## Common follow-up questions

- What is the difference between nonlocal and global?
- Why does the loop closure gotcha occur?
- What are free variables and where are they stored?

## Interview tips

- The LEGB rule must be stated clearly - interviewers check you know the exact order
- The loop closure gotcha is a very common interview question - show both fixes
- Connect closures to decorators - they use the same mechanism

## Concepts covered

Closure, LEGB, nonlocal, global, Free Variables

Previous: [concurrent.futures and Multiprocessing](https://www.internhack.in/learn/interview/python-interview/python-concurrent-futures) · Next: [classmethod vs staticmethod](https://www.internhack.in/learn/interview/python-interview/python-classmethod-staticmethod) · [All of Python Interview](https://www.internhack.in/learn/interview/python-interview)

## More Python Interview questions

- [Enum in Python](https://www.internhack.in/learn/interview/python-interview/python-enum)
- [pathlib and File System Operations](https://www.internhack.in/learn/interview/python-interview/python-pathlib)
- [Python Logging Module](https://www.internhack.in/learn/interview/python-interview/python-logging)
- [namedtuple vs NamedTuple vs dataclass](https://www.internhack.in/learn/interview/python-interview/python-namedtuple)
- [Descriptors Protocol in Depth](https://www.internhack.in/learn/interview/python-interview/python-descriptors)
- [Python Packaging and Project Setup](https://www.internhack.in/learn/interview/python-interview/python-packaging)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [Python Interview](https://www.internhack.in/learn/interview/python-interview)
