binding a reference to a variable

Denis S. Otkidach ods at fep.ru
Thu Apr 11 12:16:23 EDT 2002


On Thu, 11 Apr 2002, Patrick Miller wrote:

PM> The following does not work as intended....
PM>
PM> def f():
PM>    a = 7
PM>    L = locals()
PM>    print L.keys() # Prints ['a','L'] or some such
PM>    L['a'] = 99  # Update the value in L
PM>    print a      # But wait!  This prints out '7'

But this work and AFAIK is not implementation dependent:

>>> def set(name, val):
...     exec '%s=__val__' % name in {'__val__': val}, \
...                           sys._getframe().f_back.f_locals
...
>>> a = 7
>>> set('a', 99)
>>> a
99






More information about the Python-list mailing list