Have a look at this code:
def closure():
value = False
def method_1():
value = True
def method_2():
print 'value is:', value
method_1()
method_2()
closure()
I would expect it to print ‘Value is: True’ but it doesn’t. Why is this and what is the solution?
This happens because
method_1gets its own local scope where it can declare variables. Python seesvalue = Trueand thinks you’re creating a new variable namedvalue, local tomethod_1.The reason Python does this is to avoid polluting the outer scope’s locals with variables from an inner function. (You wouldn’t want assignments in regular, module-level functions to result in global variables being created!)
If you don’t assign to
value, then Python searches the outer scopes looking for the variable (so reading the variable works as expected, as demonstrated by yourmethod_2).One way to get around this is by using a mutable object instead of assigment:
In Python 3, the
nonlocalstatement (see also docs) was added for exactly this scenario: