Returning a value from exec or a better solution

Ethan Furman ethan at
Tue Aug 30 20:19:52 CEST 2011

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. 
  Then when the exec actually runs, any assignments, definitions, etc, 
from the user supplied string will be added.

> I spoke a bit too soon with the "works flawlessly" post.  In addition to 
> your issue, there is also the problem that supplying an empty 
> environment does not allow the user to call necessary functions (like 
> scheme_eval).

So, just like an implementation, add the functions to the CapturingDict 
before the exec.

One thing to keep in mind:  the CapturingDict only remembers the *last* 
thing created/assigned... so if the user code has more than one 
def/class/name assignment, you won't have ready access to the first 
items, only that last one.


More information about the Python-list mailing list