[Python-ideas] 'Injecting' objects as function-local constants

Alex Light scialexlight at gmail.com
Thu Jun 16 19:21:15 CEST 2011

On Thu, Jun 16, 2011 at 12:58 PM, Jan Kaliszewski <zuo at 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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20110616/35562d01/attachment.html>

More information about the Python-ideas mailing list