🔵 🔵 🔵


Primary

၊၊||၊|။

nonlocal ⚬ᵖʸ|Documentation|1st|20260122201738-00-⌔

7. Simple statements — Python 3.14.2 documentation#the-nonlocal-statement

7.13. The nonlocal statement

nonlocal_stmt: "nonlocal" identifier ("," identifier)﹡

When the definition of a function or class is nested (enclosed) within the definitions of other functions, its nonlocal scopes are the local scopes of the enclosing functions. The nonlocal statement causes the listed identifiers to refer to names previously bound in nonlocal scopes. It allows encapsulated code to rebind such nonlocal identifiers. If a name is bound in more than one nonlocal scope, the nearest binding is used. If a name is not bound in any nonlocal scope, or if there is no nonlocal scope, a SyntaxError is raised.

The nonlocal statement applies to the entire scope of a function or class body. A SyntaxError is raised if a variable is used or assigned to prior to its nonlocal declaration in the scope.

See also:
PEP 3104 - Access to Names in Outer Scopes

The specification for the nonlocal statement.

Programmer’s note: nonlocal is a directive to the parser and applies only to code parsed along with it. See the note for the global statement.

Printed 2026-06-28.

(echo:: @ )

Link to original

Secondary

• • •