In "Learning Python" by Mark Lutz I read: "Functions can freely use names assigned in syntactically enclosing functions and the global scope, but they must declare such nonlocals and globals in order to change them" I failed to test them in Python 2.7
def f1():
f1_a = 'f1_a'
def f2():
# global f1_a
# nonlocal f1_a
f2_a = 'f2_a'
print 'f2_a={:s}'.format(f2_a)
print 'f1_a={:s}'.format(f1_a)
f1_a = 'f1f2_a'
f2()
print 'f1_a={:s}'.format(f1_a)
>>> f1()
gives the error:
UnboundLocalError: local variable 'f1_a' referenced before assignment
'global' (NameError: global name 'f1_a' is not defined) and 'nonlocal' (nonlocal f1_a , SyntaxError: invalid syntax) doesn't work. Does this mean that there is no way to change a variable introduces in an outer function from the inner (immediately enclosed) one?