8000 Clarifying nonlocal doc: SyntaxError is raised if nearest enclosing scope is global by quazi-irfan · Pull Request #114009 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Clarifying nonlocal doc: SyntaxError is raised if nearest enclosing scope is global #114009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
8 changes: 5 additions & 3 deletions Doc/reference/executionmodel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,9 @@ namespace. Names are resolved in the top-level namespace by searching the
global namespace, i.e. the namespace of the module containing the code block,
and the builtins namespace, the namespace of the module :mod:`builtins`. The
global namespace is searched first. If the names are not found there, the
builtins namespace is searched. The :keyword:`!global` statement must precede
all uses of the listed names.
builtins namespace is searched. A new variable is created in the global
namespace if the name is also not found in builtins namespace. The
:keyword:`!global` statement must precede all uses of the listed names.

The :keyword:`global` statement has the same scope as a name binding operation
in the same block. If the nearest enclosing scope for a free variable contains
Expand All @@ -151,7 +152,8 @@ a global statement, the free variable is treated as a global.
The :keyword:`nonlocal` statement causes corresponding names to refer
to previously bound variables in the nearest enclosing function scope.
:exc:`SyntaxError` is raised at compile time if the given name does not
exist in any enclosing function scope. :ref:`Type parameters <type-params>`
exist in any enclosing function scope, or if the nearest enclosing scope
is the global (module) scope. :ref:`Type parameters <type-params>`
cannot be rebound with the :keyword:`!nonlocal` statement.

.. index:: pair: module; __main__
Expand Down
0