Bind compiled code to name?

"Martin v. Löwis" martin at
Thu Jun 26 18:46:29 CEST 2008

>> d = {}
>> exec source_code in d
>> some_name = d['some_name']
> This works quite well! I can't believe after googling for half on hour I 
> didn't notice this "exec ... in ..." syntax.
> One more thing though, is there a way to access "some_name" as a 
> attribute, instead as a dictionary:
> some_name = d.some_name


class D:pass
d = D()
exec source_code in d.__dict__
print d.some_name

Notice that this will also give you d.__builtins__, which you might
want to del afterwards.


More information about the Python-list mailing list