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

Steven Bethard steven.bethard at gmail.com
Wed Dec 8 19:33:52 EST 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()
3

Steve



More information about the Python-list mailing list