[Python-ideas] Alternative to PEP 532: delayed evaluation of expressions

Nathaniel Smith njs at pobox.com
Mon Nov 7 01:19:19 EST 2016


On Sun, Nov 6, 2016 at 9:08 PM, C Anthony Risinger <anthony at xtfx.me> wrote:
> On Nov 6, 2016 7:32 PM, "Nathaniel Smith" <njs at pobox.com> wrote:
>>
>> [...]
>>
>> Some other use cases:
>>
>> Log some complicated object, but only pay the cost of stringifying the
>> object if debugging is enabled:
>>
>>     log.debug!(f"Message: {message_object!r}")
>
> Would the log.debug implementation need to fetch the context to evaluate the
> delayed expression (say by using sys._getframe) or would that be bound? Is a
> frame necessary or just a (bound?) symbol table? Could a substitute be
> provided by on evaluation?

There are a lot of ways one could go about it -- I'll leave the
details to whoever decides to actually write the PEP :-) -- but one
sufficient solution would be to just pass AST objects. Those are
convenient (the compiler has just parsed the code anyway), they allow
the code to be read or modified before use (in case you want to inject
variables, or convert to SQL as in the PonyORM case, etc.), and if you
want to evaluate the thunks then you can look up the appropriate
environment using sys._getframe and friends. Maybe one can do even
better, but simple ASTs are a reasonable starting point.

-n

-- 
Nathaniel J. Smith -- https://vorpus.org


More information about the Python-ideas mailing list