This question is a follow-up on a question about Python variable scope. Additional questions q1, q2 and answers can be found on SO, among even more. The official Python documentation, and PEP 3104 are supposed to explain the details, but they don't seem fully self-explanatory to me.
The topic I'm trying to solve is refactoring of code containing nonlocal
/global
by moving that code up/down one level of hierarchy.
What I do not understand are the implications of this sentence from the Python reference:
Names listed in a nonlocal statement, unlike to those listed in a global statement, must refer to pre-existing bindings in an enclosing scope (the scope in which a new binding should be created cannot be determined unambiguously).
Given the following code on global scope:
var = 0
def outer():
global var # line A
var = 1
def inner():
nonlocal var # line B
var = 2
print('inner:', var)
inner()
print('outer:', var)
outer()
print('main:', var)
Execution raises an error:
SyntaxError: no binding for nonlocal 'var' found
The code works (with different semantics, of course, if either line A is commented out:
inner: 2
outer: 2
main: 0
or line B is commented out:
inner: 2
outer: 1
main: 1
However, in the above example, and since nonlocal
is supposed to bind var to the "enclosing scope", I would have expected that line A binds the outer/var into global scope and line B then looks for outer/var and also rebinds inner/var to global/var. Instead it seems to not find it at all (due to the rebinding in line A, I suppose) and raise an error.
The desired result I expected was:
inner: 2
outer: 2
main: 2
Is this just one more example of the confusing state of mind of scoping in Python?
Or, to make this a constructive question:
- How can such an example be written in a way that it does not matter at which level a function resides (having to exchange
global
withnonlocal
and vice versa)? - If the functions reside at an intermediate, and unknown level of hierarchy, how could the author of
outer()
change the code that neither the outermost (in this case global) level, nor theinner()
level have to be touched? -
In my humble understanding of the language, constructs like these (dependecies on closures) are just to be avoided. Others already have suggested to use other language features (classes, func attrs) to achieve this kind of context sensitivity.