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
doc: execution model - clear and complete example in documentation #48496
Comments
I'd like to propose adding some complete example regarding scopes and |
Your example seem too verbose and diffuse. Perhaps something more >>> def f():
print (x)
x = 1
>>> f()
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
f()
File "<pyshell#30>", line 2, in f
print (x)
UnboundLocalError: local variable 'x' referenced before assignment What are the other ways people get the error. |
People seem to understand that they can not use variable before def outer():
x = 1
def inner1():
print(x)
def inner2():
print(x)
# [... some instructions (maybe a lot) ...]
x = 1 They are always confused why |
The FAQ for this was much improved in 2009 (bpo-7290): http://docs.python.org/py3k/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value To support the claim that this keeps biting people, at least the following bug reports all came from people misundestanding this: [Interesting point: people mostly complain when the unbound reference occurs textually *before* the assignment (though there's selection bias here), and many of them complain that things happen "out of order". It seems half the misunderstanding is that people expect variables to *become* localized when first assigned - they don't understand it's a static decision affecting all occurences in a function.] The central problem I believe is that when people try to modify a non-local var and get Also, I'm afraid people just won't internalize this issue until it bites them at least once (I've seen a Python course where I had explained this, with a print-before-assignment example, and 2 days later a student was bitten by the exception and was at a loss. Therefore, I think providing a clear learning path from UnboundLocalError is crucial. ==> I propose (i.e. am working on patch(s)) attacking it at many points: (1) Expand UnboundLocalError message to point to help('NAMESPACES')
(2) Add an example to Naming and Binding section. (3) Improve the tutorial. Sections 4.6 [Defining functions] and 9.2 (4) Add examples to documentation of global & nonlocal? |
…n reference (pythonGH-93068) (cherry picked from commit f3db68e) Co-authored-by: Stanley <46876382+slateny@users.noreply.github.com>
…n reference (pythonGH-93068) (cherry picked from commit f3db68e) Co-authored-by: Stanley <46876382+slateny@users.noreply.github.com>
robwolfe mannequin commentedNov 1, 2008
•
edited by bedevere-bot
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: