An idiom for code generation with exec

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Mon Jun 23 03:22:29 EDT 2008


eliben a écrit :
>>    d = {}
>>  execcode in globals(), d
>>   return d['foo']
>>
>> My way:
>>
>>   return function(compile(code, '<string>', 'exec'), globals())
>>
> 
> With some help from the guys at IRC I came to realize your way doesn't
> do the same. It creates a function that, when called, creates 'foo' on
> globals(). This is not exactly what I need.

I possibly messed up a couple things in the arguments, flags etc - I 
very seldom use compile() and function(). The point was that  it didn't 
require any extra step.



More information about the Python-list mailing list