I don't understand why the following doesn't work:
def foo( x ):
n = 1
summe = 0
def bar():
n -= 1
for i in range(0,10):
y = x+i+n
x += i
summe += y
print "{0} = {1} + {2} + {3}".format(y,x,i,n)
bar()
print "summe =", summe
return summe
Why is it that bar()
doesn't inherit the scope of foo()
? Is this a C'ism that I need to forget? Is there a way I can make that work?
PEP 3104 provides an explanation and a solution for this problem. The issue is Python treats any assignment to a name as a local variable declaration.
There is a few workarounds for this problem if you use a Python version without the nonlocal keyword. One ugly trick is to wrap your variable in a list:
Although this trick works, it is usually better to rewrite the code to remove the need for non-local assignments.
I actually found this question while searching for a solution to a slightly different problem. Local variables aren't inherited by sub's inherited but there's nothing stopping you from passing the variable to the inner function and then assigning the results on return.
This is in addition to the
nonlocal
statement in PEP 3104. It's slightly less ugly and makes memorizing yet another python keyword less crucial.