How can I exec() in global?

Phil Frost indigo at bitglue.com
Sat Sep 11 14:34:09 EDT 2004


You can do exec(some_code, globals(), globals()) to execute some_code as
if it were executed at module scope. I can't really think of a reason
off hand why you would want to do this, but I assume you have your
reasons.

On Sat, Sep 11, 2004 at 09:17:02PM -0700, Jerald wrote:
> Hi.
> 
> Is it possible to exec() from within a function so the exec'd
> code affects global?
> 
> For example
> 
> def foo():
>     exec ('X=[1,2,3]')
> 
> And I'd like 'X' to be a global variable.
> 
> 
> Thanks,
> 
> Gerald



More information about the Python-list mailing list