summaryrefslogtreecommitdiffstats
path: root/Doc/reference
diff options
context:
space:
mode:
authorTerry Jan Reedy <tjreedy@udel.edu>2024-03-19 17:55:21 (GMT)
committerGitHub <noreply@github.com>2024-03-19 17:55:21 (GMT)
commit025ef7a5f7b424fba8713e448244b952bf897df3 (patch)
tree7ca69c45518e2f1bd52ab58951558cef09e8ede2 /Doc/reference
parentb85572c47dc7a8c65fc366a87a3660fc7a3ed244 (diff)
downloadcpython-025ef7a5f7b424fba8713e448244b952bf897df3.zip
cpython-025ef7a5f7b424fba8713e448244b952bf897df3.tar.gz
cpython-025ef7a5f7b424fba8713e448244b952bf897df3.tar.bz2
gh-56374: Clarify documentation of nonlocal (#116942)
Define 'nonlocal scopes' in a way that excludes class scopes. Rearrange the rest of the doc. Add "Programmer's note".
Diffstat (limited to 'Doc/reference')
-rw-r--r--Doc/reference/simple_stmts.rst30
1 files changed, 17 insertions, 13 deletions
diff --git a/Doc/reference/simple_stmts.rst b/Doc/reference/simple_stmts.rst
index 810232e..a253482 100644
--- a/Doc/reference/simple_stmts.rst
+++ b/Doc/reference/simple_stmts.rst
@@ -1006,25 +1006,29 @@ The :keyword:`!nonlocal` statement
.. productionlist:: python-grammar
nonlocal_stmt: "nonlocal" `identifier` ("," `identifier`)*
-The :keyword:`nonlocal` statement causes the listed identifiers to refer to
-previously bound variables in the nearest enclosing scope excluding globals.
-This is important because the default behavior for binding is to search the
-local namespace first. The statement allows encapsulated code to rebind
-variables outside of the local scope besides the global (module) scope.
-
-Names listed in a :keyword:`nonlocal` statement, unlike those listed in a
-:keyword:`global` statement, must refer to pre-existing bindings in an
-enclosing scope (the scope in which a new binding should be created cannot
-be determined unambiguously).
-
-Names listed in a :keyword:`nonlocal` statement must not collide with
-pre-existing bindings in the local scope.
+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 :keyword:`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 :exc:`SyntaxError` is raised.
+
+The nonlocal statement applies to the entire scope of a function or
+class body. A :exc:`SyntaxError` is raised if a variable is used or
+assigned to prior to its nonlocal declaration in the scope.
.. seealso::
:pep:`3104` - Access to Names in Outer Scopes
The specification for the :keyword:`nonlocal` statement.
+**Programmer's note:** :keyword:`nonlocal` is a directive to the parser
+and applies only to code parsed along with it. See the note for the
+:keyword:`global` statement.
+
+
.. _type:
The :keyword:`!type` statement