del
Hans Nowak
wurmy at earthlink.net
Fri Feb 14 18:54:58 EST 2003
Daniel Silva wrote:
> What is the purpose of the del statement? Why would you want to undefine
> a variable?
It's not only used for removing names from a namespace, it's also used for
deleting elements from lists, dicts, etc.
It's not uncommon to delete a name when you don't need it anymore. Especially
in modules. For example, in site.py:
for m in sys.modules.values():
if hasattr(m, "__file__") and m.__file__:
m.__file__ = os.path.abspath(m.__file__)
del m
m is only a temporary variable that isn't necessary anymore after it has done
its job. Leaving it around makes it available when site.py is imported:
import site
print site.m
which isn't really bad, but it's tidier to remove unnecessary names like that.
More precisely, how many programs would be broken if a python
> compiler did not implement it?
Lots of them. Let's see if del is used in the standard library...
(C:\Python22\Lib) $ grep " *del " *.py | wc
216 783 8643
Hmm, 216 times. I think it's safe to say that it's widely used. :-)
Cheers,
--
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA=='))
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/
Kaa:: http://www.awaretek.com/nowak/kaa.html
Soon: http://zephyrfalcon.org/
More information about the Python-list
mailing list