[Tutor] The exec command and namespaces
Jerry Hill
malaclypse2 at gmail.com
Fri Aug 10 23:05:43 CEST 2012
On Fri, Aug 10, 2012 at 4:44 PM, Jaidev Deshpande
<deshpande.jaidev at gmail.com> wrote:
> Hi,
>
> Supposed I have a string containing a python script and I exec that script.
>
> Is there a way to keep track of the variables that this exec() command creates?
Sure. You can provide the dictionaries that exec will use for globals
and locals. So, something like this might get you started:
>>> my_globals = {}
>>> my_locals = {}
>>> exec "favorite_color = 'blue'" in my_globals,my_locals
>>> print(my_locals)
{'favorite_color': 'blue'}
More information about the Tutor
mailing list