In most languages with closures, it is possible to change a closed-over value and have the change visible in other closures which share a reference to the same variable. Python's syntax makes this impossible.
Even if you're content (I am) with the work-around-- putting your changeable values inside a mutable object like a list-- it may occasionally happen that you wish you could change the closed-over values, found in "cell" objects in a function's func_closure tuple. Another recipe I submitted shows how to get at the values in standard Python; this one will demonstrate a way to actually change that value, so that functions which also close over that value (share a reference to the same cell) will see the change.
Python, 38 lines
So, how the monkey does this work?
There's no way I know of in Python to write a function that modifies one of its free variables. I'm not sure exactly why, but it's a syntactical restriction. Probably the BDFL wants to keep the rules about variable scope consistent (if you assign to a variable in a function, and the variable wasn't declared global, it's always local to the function).
Still, though, free variables go in "cell" objects, same as cellvars (variables shared by functions under the scope of a function f are cellvars in f). And the same bytecode ops work on cells either way. So until the necessary syntax is around, using straight bytecode is one way to get a cell value changed.
cell_changer_code is a code object. These can be hairy to get right, so if you're going to play around with that bit, note that you can make CPython segfault. I won't go into details here on the different values, but I will note that we create the bytecode string explicitly by concatenating the appropriate values and using the dis module to look up the right opcodes.
The change_cell_value function, then, creates a _new_ function object with cell_changer_code as its code, and puts the cell you provided in the new function's func_closure tuple. The new function needs to have a func_closure tuple of length exactly 1, by the way, since the code object's co_freevars tuple is of that length. All we have to do then is call the function and pass in the new value. The value in the cell in the closure is changed.