del

Jp Calderone exarkun at intarweb.us
Fri Feb 14 18:41:50 EST 2003


On Fri, Feb 14, 2003 at 11:02:51PM +0000, Daniel Silva wrote:
> What is the purpose of the del statement?  Why would you want to undefine 
> a variable?  More precisely, how many programs would be broken if a python 
> compiler did not implement it?

  Aside from the usage "del foo", del is also useful in other ways:

    >>> l = range(5)
    >>> del l[3] 
    >>> l
    [0, 1, 2, 4]

    >>> d = {'key': 'value'}
    >>> del d['key']
    >>> d
    {}

    >>> class Foo:
    ...    def __init__(self): self.bar = 10
    ...
    >>> f = Foo()
    >>> del f.bar
    >>> f.bar # AttributeError

    The vast majority of other cases (at least in my code) is for cleaning
up temporary variables, simply for the sake of namespace cleanliness.

    I think it is safe to say that almost every non-trivial Python program
would fail if "del" did not perform the operation it is meant to.

  Jp

-- 
   Know what I pray for? The strength to change what I can, the inability to
accept what I can't and the incapacity to tell the difference.    -- Calvin
-- 
 up 6 days, 4:29, 5 users, load average: 0.00, 0.05, 0.07
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030214/1493a3d4/attachment.sig>


More information about the Python-list mailing list