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 = {}
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 =
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
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
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
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 =
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