updating locals() and globals() (WAS: How do I do this? (eval() on the left hand side))

Steven Bethard steven.bethard at gmail.com
Thu Dec 9 01:33:52 CET 2004

Jeff Shannon wrote:
> (Note also that functions which use exec cannot use the static namespace 
> optimization, and thus tend to be *much* slower than normal functions 
> (in addition to being a huge security problem).  I don't know, however, 
> whether locals() can update the local namespace in such un-optimized 
> functions.

Doesn't look like it:

 >>> exec """\
... def f():
...     x = 3
...     locals()['x'] = 4
...     print x
... """
 >>> f()


More information about the Python-list mailing list