Bind compiled code to name?

"Martin v. Löwis" martin at v.loewis.de
Sun Jun 22 22:41:25 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

Sure:

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.

Regards,
Martin



More information about the Python-list mailing list