Unbinding multiple variables

John Hunter jdhunter at ace.bsd.uchicago.edu
Thu Jan 20 22:34:23 EST 2005


>>>>> "Johnny" == Johnny Lin <air_jlin at yahoo.com> writes:

    Johnny> Hi!  Is there a way to automate the unbinding of multiple
    Johnny> variables?  Say I have a list of the names of all
    Johnny> variables in the current scope via dir().  Is there a
    Johnny> command using del or something like that that will iterate
    Johnny> the list and unbind each of the variables?


Hi Johnny 

I assume you are the one and only Johnny Lin at the U of C, no?

John-Hunters-Computer:~> python
Python 2.3 (#1, Sep 13 2003, 00:49:11) 
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 1
>>> y = 2
>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
'__main__', 'y': 2, '__doc__': None, 'x': 1}
>>> print x,y   
1 2
>>> del locals()['x']
>>> print x,y
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'x' is not defined
>>> locals()
{'__builtins__': <module '__builtin__' (built-in)>, '__name__':
'__main__', 'y': 2, '__doc__': None}
>>> 



More information about the Python-list mailing list