How do I do this? (eval() on the left hand side)

Peter Otten __peter__ at web.de
Wed Dec 8 14:49:53 EST 2004


Caleb Hattingh wrote:

> In what way is it unreliable?  I can't seem to create a situation where
> the update through globals and locals doesn't work.   Are you referring

Updates to a locals() dictionary may not be reflected by the variables in
the local scope, e. g.:

>>> def f():
...     locals()["a"] = 1
...     print a
...
>>> f()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in f
NameError: global name 'a' is not defined
>>> def f():
...     a = 42
...     locals()["a"] = 1
...     print a
...
>>> f()
42

Updating globals() should be safe.
 
Peter




More information about the Python-list mailing list