Scope of Variables in Python — a practical guide to Python variable scope with clear examples you can reuse in real projects.
Python Tutorial Series (32/95). Prefer one article? Read the complete Python tutorial.
Short description
Names resolve using LEGB. Use `global`/`nonlocal` carefully when you must rebind outer names.
Scope demo
x = 'global'
def outer():
x = 'enclosing'
def inner():
print(x) # enclosing
inner()
outer()
print(x)