![]() | ![]() |
A Python’s nonlocal variable is used in a nested function whose local scope is unspecified. It is neither global nor local scope.
Example
This example shows how to create a nonlocal variable.
Start IDLE.
Navigate to the File menu and click New Window.
Type the following:
def outer():
y = "local variable"
def inner():
nonlocal y
y = "nonlocal variable"
print("inner:", y)
inner()
print("outer scope:", y)
outer()