Modify bound variables of a closure in Python Modify bound variables of a closure in Python python python

Modify bound variables of a closure in Python


It is quite possible in python 3 thanks to the magic of nonlocal.

def foo():        var_a = 2        var_b = 3        def _closure(x, magic = None):                nonlocal var_a                if magic is not None:                        var_a = magic                return var_a + var_b + x        return _closurelocalClosure = foo()# Local closure is now "return 2 + 3 + x"a = localClosure(1) # 2 + 3 + 1 == 6print(a)# DO SOME MAGIC HERE TO TURN "var_a" of the closure into 0localClosure(0, 0)# Local closure is now "return 0 + 3 + x"b = localClosure(1) # 0 + 3 +1 == 4print(b)


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:

class foo:        def __init__(self):                self.var_a = 2                self.var_b = 3        def __call__(self, x):                return self.var_a + self.var_b + xlocalClosure = 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?localClosure.var_a = 0# Local closure is now "return 0 + 3 + x"b = localClosure(1) # 0 + 3 +1 == 4

If you do use this technique I would no longer use the name localClosure because it is no longer actually a closure. However, it works the same as one.


I've found an alternate answer answer to Greg's, slightly less verbose because it uses Python 2.1's custom function attributes (which conveniently enough can be accessed from inside their own function).

def foo():    var_b = 3    def _closure(x):        return _closure.var_a + var_b + x    _closure.func_dict['var_a'] = 2    return _closurelocalClosure = 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?# apparently, it islocalClosure.var_a = 0# Local closure is now "return 0 + 3 + x"b = localClosure(1) # 0 + 3 +1 == 4

Thought I'd post it for completeness. Cheers anyways.