Unbinding multiple variables

Steven Bethard steven.bethard at gmail.com
Fri Jan 21 19:37:29 EST 2005


Alex Martelli wrote:
> Nevertheless, any modifications to locals() are utterly
> futile (within a function).

Evil hack that makes modifications to locals() not quite as futile:

py> import sys
py> import ctypes
py> def f():
...     x = 1
...     locals()['x'] = 2
...     ctypes.pythonapi.PyFrame_LocalsToFast(
...         ctypes.py_object(sys._getframe()), 0)
...     return x
...
py> f()
2

Warning!  NEVER do this! ;)

(Also note that you can't del or add variables in this manner -- only 
modify them.)

Steve



More information about the Python-list mailing list