Skip to content

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

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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. If the name is also not in the builtins namespace,
a new variable is created in the global 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