The del statement

Terry Reedy tjreedy at udel.edu
Thu May 8 13:47:45 EDT 2008


"George Sakkis" <george.sakkis at gmail.com> wrote in message 
news:02860f2a-cd1d-4b1d-ad49-08b13032ba21 at 2g2000hsn.googlegroups.com...
| One of the few Python constructs that feels less elegant than
| necessary to me is the del statement. For one thing, it is overloaded
| to mean three different things:
| (1) del x: Remove x from the current namespace
| (2) del x[i]: Equivalent to x.__delitem__(i)
| (3) del x.a: Equivalent to x.__delattr__('a') (or delattr(x,'a'))

Since I see del x.a as deleting a from the attribute namespace of x, I see 
this as the same meaning.  A namespace is a specialized association (keys 
are identifier strings).  A dict is more generalized (keys merely 
hashable).  So ditto for del dic[key].

The only different meaning is del somelist[i].  The implicit association 
between counts in range(n=len(somelist)) *is* broken, but unless i == n-1, 
items are 'shifted down' so that some other item become associated with i, 
and j's for i < j < n-1 get new associations and n-1 is left with none. 
One could imagine del somelist[i] as simple leaving a void in the list. 
(Which is not to say that that would be terribly useful.)

tjr






More information about the Python-list mailing list