-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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] clarify documentation of nonlocal #56374
Comments
Can "nonlocal x" declaration also reference global x? I would assume that global scope automatically encloses all other scopes. But the nonlocal keyword seems to exclude global scope. This seems counter-intuitive. (maybe bug in implementation?) For example: x=0
def count(i):
nonlocal x; x+=i; return x; Checking this code in IDLE gives: I am coming to Python from Lisp, so I am still learning Python's principles. |
Does this doc help: http://docs.python.org/dev/reference/simple_stmts#the-nonlocal-statement ? |
Well, I read that text before, but it may be because I am not native english speaker that I interpret the text differently. Let's go through it one step at a time and show me where I am wrong.
Nothing specifically says that global bindings are excluded. |
Lukas, I'll fix-up the docs. Thanks for pointing out the deficiency. |
Another problem with the current text is that it fails to exclude enclosing class scopes, and I had to test to be sure they were., and some of the phrasing strikes me as awkward. Here is a possible rewrite. "When the definition of a function is nested (enclosed) within the definitions of other functions, its nonlocal scopes are the local scopes of the enclosing functions. The nonlocal statement causes the listed identifiers to refer to names previously bound in nonlocal scopes. 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 SyntaxError is raised. Except for the requirement that the listed indentifiers be previously bound, the nonlocal statement is similar to the global statement. It applies to the entire function body, so it cannot follow any local bindings of the same names." My main change is to first unambiguously define nonlocal scopes and continue from there. |
The doc has been updated by now and mentions "excluding globals". One comment on his suggestion: I would remove/change "the nonlocal statement is similar to the global statement" because it's not obvious in what way it is similar just from the sentence, I think this is more confusing then helpful. |
I concur. |
The nonlocal statement is only disallowed in module code (i.e. "exec" compilation mode) because it can never be nested. It's allowed in a class definition that has an outer function scope. A class body itself is never a nonlocal scope; it just has access to them. Here's a slightly modified version that includes class definitions: "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 nonlocal statement causes the listed identifiers to refer to names previously bound in nonlocal scopes. 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 SyntaxError is raised. The nonlocal statement applies to the entire scope of a function or class body. A SyntaxError is raised if a variable is used or assigned to prior to its nonlocal declaration in the scope." |
Redefine 'nonlocal scopes' in a way that excludes class scopes. Rearrange the rest of the doc. Add "Programmer's note".
Define 'nonlocal scopes' in a way that excludes class scopes. Rearrange the rest of the doc. Add "Programmer's note".
Define 'nonlocal scopes' in a way that excludes class scopes. Rearrange the rest of the doc. Add "Programmer's note". (cherry picked from commit 025ef7a) Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Not backported to 3.11 due to merge conflict. |
Define 'nonlocal scopes' in a way that excludes class scopes. Rearrange the rest of the doc. Add "Programmer's note".
Define 'nonlocal scopes' in a way that excludes class scopes. Rearrange the rest of the doc. Add "Programmer's note".
Define 'nonlocal scopes' in a way that excludes class scopes. Rearrange the rest of the doc. Add "Programmer's note".
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: