nonlocal — Expert Examples
Declares a variable inside a nested function as belonging to the enclosing scope
nonlocal bytecode: cell variables
How nonlocal works with cell and free variables.
python
import dis def outer(): x = 10 def inner(): nonlocal x x += 1 return x return inner print("outer bytecode:") dis.dis(outer) print("\ninner bytecode:") dis.dis(outer()) # outer stores x as a cell variable (LOAD_DEREF/STORE_DEREF) # inner accesses x as a free variable (also LOAD_DEREF/STORE_DEREF) # Both use the same cell object, enabling shared state fn = outer() print(f"\nFree vars: {fn.__code__.co_freevars}") print(f"Closure: {fn.__closure__}") print(f"Cell contents: {fn.__closure__[0].cell_contents}")
nonlocal variables use cell objects shared between the enclosing and inner function. Both access the cell via LOAD_DEREF/STORE_DEREF opcodes. The cell is stored in __closure__.
Want to try these examples interactively?
Open Expert Playground