This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: [doc] clarify documentation of nonlocal
Type: behavior Stage: patch review
Components: Documentation Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Lukas.Petru, eric.araujo, eryksun, iritkatriel, nanjekyejoannah, rhettinger, slateny, terry.reedy
Priority: normal Keywords: easy, patch

Created on 2011-05-24 05:57 by Lukas.Petru, last changed 2022-04-11 14:57 by admin.

Pull Requests
URL Status Linked Edit
PR 31551 open slateny, 2022-02-24 08:58
Messages (8)
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.
msg408404 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-12-12 18:50
The doc has been updated by now and mentions "excluding globals".
I'm changing the title to reflect Terry's last comment.

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.
msg413956 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2022-02-25 02:37
> I think this is more confusing then helpful.

I concur.
msg413966 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2022-02-25 06:22
> Another problem with the current text is that it fails to exclude 
> enclosing class scopes

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."
History
Date User Action Args
2022-04-11 14:57:17adminsetgithub: 56374
2022-02-25 06:22:06eryksunsetnosy: + eryksun
messages: + msg413966
2022-02-25 02:37:26rhettingersetmessages: + msg413956
2022-02-24 08:58:14slatenysetkeywords: + patch
nosy: + slateny

pull_requests: + pull_request29672
stage: needs patch -> patch review
2021-12-12 18:50:45iritkatrielsetnosy: + iritkatriel
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.4
messages: + msg408404

keywords: + easy, - patch
title: Nonlocal does not include global; clarify doc -> [doc] clarify documentation of nonlocal
2020-07-05 02:41:26nanjekyejoannahsetnosy: + nanjekyejoannah

stage: needs patch
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