changing local namespace of a function

Bo Peng bpeng at rice.edu
Fri Feb 4 19:28:34 EST 2005


M.E.Farmer wrote:

>>>>def fun(d):
> 
> ...     __dict__ = d
> ...     return __dict__
> hth,


Does not work?

 >>> a = { 'x':1, 'y':2}
 >>> b = { 'x':2, 'y':9}
 >>> def fun(d):
...   __dict__ = d
...   print locals()
...   z = x + y
 >>> fun(a)
{'__dict__': {'y': 2, 'x': 1}, 'd': {'y': 2, 'x': 1}}
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
   File "/usr/tmp/python-6377tax.py", line 4, in fun
NameError: global name 'x' is not defined



More information about the Python-list mailing list