make __closure__ writable

Can we make the __closure__ attribute writeable? Since __code__ already is, and it is possible to tackle with the opcodes, having no way of rewriting __closure__ (except creating a completely new function) is annoying. I don't think it will somehow harm python, as those who want to break it can do it already in multiple ways, easier than playing with __closure__. - Yury

Decorators can be nested, and what you can do in this case is to find the most inner-wrapped function by traversing the '__wrapped__' attributes (and check that the function you found is the actual original function). After that you can play with its attributes, but you can't simply substitute the function object, as the inner decorator won't use it. So sometimes you have to work with the function object without a way of substituting it. - Yury On 2012-03-16, at 2:37 PM, Mark Shannon wrote:

On Fri, Mar 16, 2012 at 1:57 PM, Yury Selivanov <yselivanov.ml@gmail.com> wrote:
+1000, this would make code like https://github.com/magcius/toenail/blob/master/ailment.py#L20 much cleaner and more readable. Mike

On 26 March 2012 11:32, Victor Stinner <victor.stinner@gmail.com> wrote:
There are many things you can do with bytecode manipulation (whether you should is another question). Among other things, I've used it for optimisation (e.g. my optimised self.super recipe that probably isn't actually available online anymore). Instrumentation of code is another thing, although these days you're probably better off using a decorator. There aren't a lot of real use cases, but if nothing else it can be a lot of fun :) Tim Delaney

Decorators can be nested, and what you can do in this case is to find the most inner-wrapped function by traversing the '__wrapped__' attributes (and check that the function you found is the actual original function). After that you can play with its attributes, but you can't simply substitute the function object, as the inner decorator won't use it. So sometimes you have to work with the function object without a way of substituting it. - Yury On 2012-03-16, at 2:37 PM, Mark Shannon wrote:

On Fri, Mar 16, 2012 at 1:57 PM, Yury Selivanov <yselivanov.ml@gmail.com> wrote:
+1000, this would make code like https://github.com/magcius/toenail/blob/master/ailment.py#L20 much cleaner and more readable. Mike

On 26 March 2012 11:32, Victor Stinner <victor.stinner@gmail.com> wrote:
There are many things you can do with bytecode manipulation (whether you should is another question). Among other things, I've used it for optimisation (e.g. my optimised self.super recipe that probably isn't actually available online anymore). Instrumentation of code is another thing, although these days you're probably better off using a decorator. There aren't a lot of real use cases, but if nothing else it can be a lot of fun :) Tim Delaney
participants (6)
-
Joao S. O. Bueno
-
Mark Shannon
-
Mike Graham
-
Tim Delaney
-
Victor Stinner
-
Yury Selivanov