del and sets proposal

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Fri Oct 3 05:29:00 EDT 2008


On Fri, 03 Oct 2008 02:18:53 -0700, Carl Banks wrote:

> On Oct 2, 11:27 pm, Larry Bates <larry.ba... at vitalEsafe.com> wrote:
>> I didn't mean to imply that del a[1] would delete the first thing in
>> the set, but rather the item with a value of 1.  Just as when we use it
>> on a dictionary:
>>
>> del a[1]
>>
>> doesn't mean delete the first dictionary entry but rather delete the
>> entry in the object with a value of 1, which IMHO would be perfectly
>> logical for a set (which is why I started this discussion).
> 
> 
> It's not logical at all.  In all current uses of del, the thing that
> follows del is a valid expression.  With sets, that's not the case.


I think Larry is suggesting that elements of sets should be removed in 
the same way that keys of dictionaries are removed:

d = {57: "foo"}
s = set([57])


He's suggesting that del s[57] should work just like del d[57] works.

The fact that sets don't have a __getitem__ method doesn't mean that they 
couldn't have a __delitem__ method:

class DelSet(set):
    def __delitem__(self, element):
        self.remove(element)


>>> s = DelSet([1, 2, 3, 4, 5])
>>> s
DelSet([1, 2, 3, 4, 5])
>>> del s[4]
>>> del s[5]
>>> s
DelSet([1, 2, 3])




-- 
Steven



More information about the Python-list mailing list