[Python-ideas] make __closure__ writable
Mark Shannon
mark at hotpy.org
Thu Mar 29 10:05:37 CEST 2012
Tim Delaney wrote:
> On 26 March 2012 11:32, Victor Stinner <victor.stinner at gmail.com
> <mailto:victor.stinner at gmail.com>> wrote:
>
> 2012/3/16 Yury Selivanov <yselivanov.ml at gmail.com
> <mailto:yselivanov.ml at gmail.com>>:
> > Can we make the __closure__ attribute writeable? Since __code__
> already is, ...
>
> I never understood why __code__ is writable. What is the usecase of
> modifying the code of an existing function?
>
>
> 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 :)
You can do all of those things without changing the __code__ attribute.
Just create a new function instead.
Cheers,
Mark
More information about the Python-ideas
mailing list