I'm confused by the behavior of python's nonlocal
keyword.
So the standard example is as in this question: there is a def outerfoo():
block containing a variable definition outvar = 0
, and inside outerfoo()
there is a def innerfoo():
block containing nonlocal outvar
, making it so that the value of outvar
in outerfoo():
's namespace can be changed by what goes on inside innerfoo()
.
So far so good. However, the following code fails with a SyntaxError
:
outvar = 0 #not in any function
def foo():
nonlocal outvar
outvar += 1
foo()
What namespace is the parser looking in, that it is not finding outvar
?