classification
Title: Nonlocal does not include global; clarify doc
Type: behavior Stage:
Components: Documentation Versions: Python 3.4
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Lukas.Petru, eric.araujo, rhettinger, terry.reedy
Priority: normal Keywords: patch

Created on 2011-05-24 05:57 by Lukas.Petru, last changed 2013-07-12 06:48 by rhettinger.

Messages (5)
msg136722 - (view) Author: Lukas Petru (Lukas.Petru) Date: 2011-05-24 05:57
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: no binding for nonlocal 'x' found. If this is what was intended, maybe it should be more clearly written in the documentation.

I am coming to Python from Lisp, so I am still learning Python's principles.
msg136766 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-05-24 16:34
Does this doc help: http://docs.python.org/dev/reference/simple_stmts#the-nonlocal-statement ?
msg136834 - (view) Author: Lukas Petru (Lukas.Petru) Date: 2011-05-25 09:47
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.

1) The nonlocal statement causes the listed identifiers to refer to previously bound variables in the nearest enclosing scope.

- I assume here that global scope is an enclosing scope (global encloses everything in the module).

2) The statement allows encapsulated code to rebind variables outside of the local scope besides the global (module) scope.

- Here I interpret "besides" as "in addition to".

Nothing specifically says that global bindings are excluded.
msg136877 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-05-25 17:39
Lukas, I'll fix-up the docs.  Thanks for pointing out the deficiency.
msg136882 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2011-05-25 18:32
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.
History
Date User Action Args
2013-07-12 06:48:27rhettingersetassignee: rhettinger ->
versions: + Python 3.4, - Python 3.2, Python 3.3
2011-05-25 18:32:53terry.reedysettitle: Does nonlocal include global? -> Nonlocal does not include global; clarify doc
2011-05-25 18:32:00terry.reedysetkeywords: + patch
nosy: + terry.reedy
messages: + msg136882

2011-05-25 17:39:08rhettingersetversions: + Python 3.3
nosy: + rhettinger

messages: + msg136877

assignee: rhettinger
components: + Documentation, - Interpreter Core
2011-05-25 09:47:09Lukas.Petrusetmessages: + msg136834
2011-05-24 16:34:50eric.araujosetnosy: + eric.araujo
messages: + msg136766
2011-05-24 05:57:36Lukas.Petrucreate