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?

Edit
Report