On Thu, Jun 16, 2011 at 12:58 PM, Jan Kaliszewski <zuo@chopin.edu.pl> wrote:
Steven D'Aprano dixit (2011-06-16, 10:46):
> This implies to me that inject (option 1) must copy the original function and
> make modifications to the code object. This sounds to me that a
> proof-of-concept implementation would be doable using a byte-code
> hack.

That's what I propose as variant #2. But that would need byte code
hacking -- or some core language ('magic') modifications.

I agree with D'Aprano, both option one and two would require modifying the code object,
bytecode hacks, or core changes to the language, because, although the result is the 
same as your factory example the function is already compiled when it is given to the

my best guess is that to implement this we would need to slightly redefine the way that 
python looks up variables by making it look in a special 'injected' dictionay after looking 
through locals and before globals.