Namespaces, multiple assignments, and exec()

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Dec 20 04:15:51 CET 2008


On Sat, 20 Dec 2008 02:53:16 +0000, MRAB wrote:

> If you're sure you want to use the current namespace then:
> 
>      for name in namelist:
>          vars()[name] = func(name, args)

Doesn't work inside a function:

>>> def parrot():
...     for name in ['A', 'B', 'C']:
...             vars()[name] = ord(name)
...     print vars()
...     print A
...
>>> parrot()
{'A': 65, 'C': 67, 'B': 66, 'name': 'C'}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in parrot
NameError: global name 'A' is not defined



-- 
Steven



More information about the Python-list mailing list