In the following Python code, I get an UnboundLocalError
. As I understand it, local functions share the local variables of the containing function, but this hardly seems to be the case here. I recognise that a
is an immutable value in this context, but that should not be a problem.
def outer():
a = 0
def inner():
a += 1
inner()
outer()
It would seem that the inner function has received copies of all the references in the parent function, as I do not get the UnboundLocalError
exception if the value of a
is wrapped in a mutable type.
Is someone able to clarify the behaviour here, and point me to the appropriate Python documentation on this?
You should specify your variable as nonlocal to preserve it's state in closure, so definition should be like this
I believe you're correct in seeing this as a "mutability" problem. While the code you posted does throw an "UnboundLocalError", the following code does not:
Python doesn't allow you to reassign the value of a variable from an outer scope in an inner scope (unless you're using the keyword "global", which doesn't apply in this case).
Check out the bottom section of the "classes" documentation in this Python 2.6.2 documentation:
Your "UnboundLocalError" is because your function is actually declaring a new variable called "a" and then immediately trying to do a "+=" operation on it, but this fails because "a" does not have a value yet. (View the "a+=1" as "a = a+1" and you can see the problem if "a" is undefined).
In general, if you're going to want to modify "a", the way people usually get around it is to use a mutable type to pass "a" around (such as a list or a dictionary). You can modify "a" via the contents of the mutable type (as you probably noticed in your testing with this setup).
Hope that helps!
Try binding the variable as an argument.
I'll try and dig up the appropriate documents.
edit
Since you want the inner function to have a side effect on the outer scope, then you need to use a mutable datatype like a list. Integers and strings are immutable.