Tag Archives: nonlocal Statement in Python 3

The nonlocal Statement in Python 3

In Python 3 we can change enclosing scope variables by declaring them in nonlocal statements, so nested defs can have access to names in enclosing functions.

  • The nonlocal declares that a name will be changed in an enclosing scope.
  • The nonlocal applies to a name in an enclosing function’s scope.
  • The nonlocal names must already exist in the enclosing function’s scope when declared. They can exist only in enclosing functions and cannot be created by a first assignment in a nested def.

Continue reading