[Python-ideas] 'Injecting' objects as function-local constants
ericsnowcurrently at gmail.com
Fri Jun 17 08:13:57 CEST 2011
On Thu, Jun 16, 2011 at 11:37 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> What benefit is there in making inject a keyword? Even super isn't a
> As far as I'm concerned, inject need only be a function in the functools
> module, not even a built-in, let alone a keyword.
If inject is a decorator then it would be used to inject into the
locals of any function at runtime. This is in contrast to Nick's
proposal where it's strictly tied to definition time and injection is
internal to to the subject of the injection, the function body.
In the latter case, the resulting code object from the function body
could incorporate the injection right there. To accomplish the same
thing with a decorator, the function definition would have to know
about any possible decoration before the code object is compiled (good
luck), or the decorator would replace/modify the compiled code. Seems
like that's not far off from what Jan was proposing.
The alternative is to have the injection handled externally to the
compiled code object, like a co_static on the code object of a
__static__ on the function object. Then the execution of the function
code object would pull that in.
More information about the Python-ideas