From 8e34c351da29bd3e6f2a159b38bc7aadcb7e9ad2 Mon Sep 17 00:00:00 2001 From: slateny <46876382+slateny@users.noreply.github.com> Date: Sun, 22 May 2022 03:18:47 -0700 Subject: [PATCH 1/4] Added example and link to faq for UnboundLocalError in reference --- Doc/faq/programming.rst | 2 ++ Doc/reference/executionmodel.rst | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/Doc/faq/programming.rst b/Doc/faq/programming.rst index f87eaff9531fce..4deeaa72ca3375 100644 --- a/Doc/faq/programming.rst +++ b/Doc/faq/programming.rst @@ -111,6 +111,8 @@ Yes. The coding style required for standard library modules is documented as Core Language ============= +.. _faq-unboundlocalerror: + Why am I getting an UnboundLocalError when the variable has a value? -------------------------------------------------------------------- diff --git a/Doc/reference/executionmodel.rst b/Doc/reference/executionmodel.rst index d9183561820b2b..860faecfdeb8c1 100644 --- a/Doc/reference/executionmodel.rst +++ b/Doc/reference/executionmodel.rst @@ -125,9 +125,23 @@ used, an :exc:`UnboundLocalError` exception is raised. If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule -is subtle. Python lacks declarations and allows name binding operations to +is subtle:: + + >>> x = 1 + >>> def new_scope(): + ... print(x) + ... x = 2 + ... + >>> new_scope() + Traceback (most recent call last): + File "", line 1, in + File "", line 2, in new_scope + UnboundLocalError: local variable 'x' referenced before assignment + +Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. +See also :ref:`the FAQ entry on UnboundLocalError `. If the :keyword:`global` statement occurs within a block, all uses of the names specified in the statement refer to the bindings of those names in the top-level From 260e17104b696f5c7ae05cd8f76566acc12066c3 Mon Sep 17 00:00:00 2001 From: slateny <46876382+slateny@users.noreply.github.com> Date: Fri, 9 Dec 2022 20:17:41 -0800 Subject: [PATCH 2/4] Remove example as FAQ already contains one --- Doc/reference/executionmodel.rst | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/Doc/reference/executionmodel.rst b/Doc/reference/executionmodel.rst index 860faecfdeb8c1..8cae95e1e5578c 100644 --- a/Doc/reference/executionmodel.rst +++ b/Doc/reference/executionmodel.rst @@ -125,23 +125,11 @@ used, an :exc:`UnboundLocalError` exception is raised. If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule -is subtle:: - - >>> x = 1 - >>> def new_scope(): - ... print(x) - ... x = 2 - ... - >>> new_scope() - Traceback (most recent call last): - File "", line 1, in - File "", line 2, in new_scope - UnboundLocalError: local variable 'x' referenced before assignment - -Python lacks declarations and allows name binding operations to +is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. -See also :ref:`the FAQ entry on UnboundLocalError `. +See :ref:`the FAQ entry on UnboundLocalError ` for some +examples. If the :keyword:`global` statement occurs within a block, all uses of the names specified in the statement refer to the bindings of those names in the top-level From 7026c3ab9fc0f59c5bdd0a88c54606a5c1c02ddd Mon Sep 17 00:00:00 2001 From: slateny <46876382+slateny@users.noreply.github.com> Date: Fri, 9 Dec 2022 20:18:15 -0800 Subject: [PATCH 3/4] Readd space --- Doc/reference/executionmodel.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/reference/executionmodel.rst b/Doc/reference/executionmodel.rst index 8cae95e1e5578c..c0941cbcb319d6 100644 --- a/Doc/reference/executionmodel.rst +++ b/Doc/reference/executionmodel.rst @@ -125,7 +125,7 @@ used, an :exc:`UnboundLocalError` exception is raised. If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule -is subtle. Python lacks declarations and allows name binding operations to +is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. See :ref:`the FAQ entry on UnboundLocalError ` for some From 643ead65bd994ea24f5459bbdd2717afff302e54 Mon Sep 17 00:00:00 2001 From: Stanley <46876382+slateny@users.noreply.github.com> Date: Wed, 21 Dec 2022 17:28:06 -0800 Subject: [PATCH 4/4] Simplify wording Co-authored-by: C.A.M. Gerlach --- Doc/reference/executionmodel.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/reference/executionmodel.rst b/Doc/reference/executionmodel.rst index c0941cbcb319d6..081f71cf2e7d97 100644 --- a/Doc/reference/executionmodel.rst +++ b/Doc/reference/executionmodel.rst @@ -128,8 +128,8 @@ lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. -See :ref:`the FAQ entry on UnboundLocalError ` for some -examples. +See :ref:`the FAQ entry on UnboundLocalError ` +for examples. If the :keyword:`global` statement occurs within a block, all uses of the names specified in the statement refer to the bindings of those names in the top-level