Is there any way to modify the bound value of one of the variables inside a closure? Look at the example to understand it better.
def foo(): var_a = 2 var_b = 3 def _closure(x): return var_a + var_b + x return _closure localClosure = foo() # Local closure is now 'return 2 + 3 + x' a = localClosure(1) # 2 + 3 + 1 == 6 # DO SOME MAGIC HERE TO TURN 'var_a' of the closure into 0 # ...but what magic? Is this even possible? # Local closure is now 'return 0 + 3 + x' b = localClosure(1) # 0 + 3 +1 == 4
I don’t think there is any way to do that in Python. When the closure is defined, the current state of variables in the enclosing scope is captured and no longer has a directly referenceable name (from outside the closure). If you were to call
foo()again, the new closure would have a different set of variables from the enclosing scope.In your simple example, you might be better off using a class:
If you do use this technique I would no longer use the name
localClosurebecause it is no longer actually a closure. However, it works the same as one.