Re: Bind compiled code to name?

2008-06-26 Thread Martin v. Löwis
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 =

Re: Bind compiled code to name?

2008-06-26 Thread Martin v. Löwis
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 =

Bind compiled code to name?

2008-06-22 Thread Karlo Lozovina
If string `source_code` contains Python source code, how can I execute that code, and bind it to some name? I tried compiling with: code_object = compile(source_code, 'errorfile', 'exec') but then what to do with `code_object`? P.S. If my intentions aren't that clear, this is what I'm

Re: Bind compiled code to name?

2008-06-22 Thread Martin v. Löwis
If string `source_code` contains Python source code, how can I execute that code, and bind it to some name? I tried compiling with: code_object = compile(source_code, 'errorfile', 'exec') but then what to do with `code_object`? P.S. If my intentions aren't that clear, this is

Re: Bind compiled code to name?

2008-06-22 Thread Karlo Lozovina
Martin v. Löwis [EMAIL PROTECTED] wrote in news:[EMAIL PROTECTED]: 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

Re: Bind compiled code to name?

2008-06-22 Thread Martin v. Löwis
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 =

Re: Bind compiled code to name?

2008-06-22 Thread Giuseppe Ottaviano
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. If you want to mimic an import you can also do this: import types D = types.ModuleType('D') exec source_code in D.__dict__ print