Language Syntax
Nonlocal — Python syntax
Rebind a name in the nearest enclosing function scope that already binds it. It cannot create a new enclosing binding or target the global namespace.
Formal Syntax & Structure
python syntaxpython
nonlocal name [, name ...]Working Example
Example Snippetpython
def counter(): n=0 def inc(): nonlocal n n+=1 return n return incf=counter()print((f(), f()))