10000 gh-48496: Added example and link to faq for UnboundLocalError in reference by slateny · Pull Request #93068 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-48496: Added example and link to faq for UnboundLocalError in reference #93068

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
merged 4 commits into from
Dec 22, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove example as FAQ already contains one
  • Loading branch information
slateny committed Dec 10, 2022
commit 260e17104b696f5c7ae05cd8f76566acc12066c3
18 changes: 3 additions & 15 deletions Doc/reference/executionmodel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<stdin>", line 1, in <module>
File "<stdin>", 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 <faq-unboundlocalerror>`.
See :ref:`the FAQ entry on UnboundLocalError <faq-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
Expand Down
0