11

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.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
cfi
  • 10,915
  • 8
  • 57
  • 103
  • 1
    Global and local variables are fundamentally different in Python. `nonlocal` statements can only refer to local variables of an enclosing scope and never to global variables -- that's what `global` is for after all. The concepts behind `global` and `nonlocal` are rather different. Your arguments how the current state should pose a problem are entirely constructed. My answer would be: Don't use global variables, and don't use `global`, and all mentioned "problems" (and a few more) will be gone. – Sven Marnach Nov 08 '11 at 13:07
  • While I generally agree to avoid using globals, others seem to find the concept so useful that Python provides the `global` keyword and Py3k even added a `nonlocal`. I'm trying to understand the implications of their combined use. – cfi Nov 08 '11 at 14:15
  • 1
    "… and Py3k even added a `nonlocal`." `nonlocal` is meant to make closures more useful. It is unrelated to global variables. – Sven Marnach Nov 08 '11 at 14:22
  • Does this answer your question? [Why doesn't Python's nonlocal keyword like the global scope?](https://stackoverflow.com/questions/16873615/why-doesnt-pythons-nonlocal-keyword-like-the-global-scope) – Karl Knechtel Sep 11 '22 at 09:58

2 Answers2

22

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.

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
  • "global and nonlocal are not meant to be combined" and that's exactly my problem: If not, then refactoring of code is made more difficult. Given that an innermost function uses a `nonlocal` and the immediate surrounding function as well. If the surrounding function changes the binding to `global`, then the innermost function must be changed, too. But, alas, with the syntax at hand I don't see another way. (But to use class wrappers and such) – cfi Nov 09 '11 at 08:51
  • I see your point, but it does not seem to me to be an unreasonable amount of work if you are refactoring closures into non-closures. – Ethan Furman Nov 09 '11 at 17:10
0

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.

newacct
  • 119,665
  • 29
  • 163
  • 224
  • My point is about refactoring: If the middle function together with its inner function is moved one level lower, a `nonlocal` would have to be changed in `global`, even if its immediate surrounding function would also make use of the variable. Hm, I start to think I've chosen a bad example for detailing the problem I have with this syntax/scoping. – cfi Nov 09 '11 at 08:47
  • @cfi I just want to make sure I understand what you mean by: "If the middle function together with its inner function is moved one level lower". Initially, both `var` and `outer` are inside another function's block, therefore both `outer` and `inner` use `nonlocal var`. By moving the "middle function together with its inner function one level lower" you mean moving both `var` and `outer` closer to the global scope? And if this new scope for `var` and `outer` were the global scope, you would have to change both the `nonlocal var` in `outer` and in `inner` to `global var`? – user42768 Nov 12 '17 at 09:17