An idiom for code generation with exec

eliben eliben at gmail.com
Sat Jun 21 02:10:16 EDT 2008


>    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.

Eli



More information about the Python-list mailing list