Python's nonlocal depends on level of hierarch

2019-03-26 12:18发布

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 with nonlocal 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 the inner() 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.

2条回答
爷、活的狠高调
2楼-- · 2019-03-26 13:00

global and nonlocal are not meant to be combined. They mean different things:

  • global means the name exists at the module level
  • nonlocal means the name exists in an outer lexical function scope

The reason you are getting the original exception is because you told Python that var is nonlocal (meaning it is in an outer function definition), but there is no function-level binding for var in any outer function definition because you told Python in the outer function that var was global.

查看更多
Luminary・发光体
3楼-- · 2019-03-26 13:16

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 with nonlocal and vice versa)?

It does not matter at which level a function resides. It only matters at which level the variable resides.

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 the inner() level have to be touched?

You are asking whether it is possible for a function at an intermediate level to, by changing something in its code, to cause a variable in an inner function to alternately change something at a global scope or something in the outer function's local scope. This seems like a really weird thing to be able to do.

查看更多
登录 后发表回答