Returning a value from exec or a better solution

Ethan Furman ethan at stoneleaf.us
Tue Aug 30 22:59:34 CEST 2011


Rob Williscroft wrote:
> Ethan Furman wrote in news:4E5D29C8.8010206 at stoneleaf.us in 
> gmane.comp.python.general:
> 
>> Jack Trades wrote:
>>> On Tue, Aug 30, 2011 at 2:37 AM, Rob Williscroft wrote:
>>>> If an impementation (as you say up thread) can populate globals
>>>> or locals with whatever they want, then how do you know that last
>>>> item added was the function definition the user supplied ?
>> Because the implementation will add things before the exec is processed. 
> 
> How do you know this ?, it isn't what the docs say.
> 
> http://docs.python.org/reference/simple_stmts.html#the-exec-statement

The docs don't say when, true -- so I don't know for sure that all 
additions happen before the exec since I haven't delved into the source 
code...

Okay, just perused builtin_exec in bltinmodule.c, and I can say that 
cPython (somewhere in 3.2-3.3 land) only adds __builtins__ if not 
already there, and that it does it first.

Which makes sense -- after all, what's the point of adding stuff to 
globals() *after* the code has been run?

~Ethan~



More information about the Python-list mailing list