del and sets proposal

Bruno Desthuilliers bdesth.quelquechose at
Sun Oct 5 16:22:18 CEST 2008

Steven D'Aprano a écrit :
> On Sun, 05 Oct 2008 22:11:38 +1300, Lawrence D'Oliveiro wrote:
>> In message <00f80c6a$0$20633$c3e8da3 at>, Steven D'Aprano
>> wrote:
>>> Would it really be "confusing" if sets used the same interface as dicts
>>> use? I don't think so. What else could "del aset[x]" mean other than
>>> "delete element x"?
>> Yes, but "x" in what sense? 
> The only sense possible.
> If I wrote aset.remove(x) would you ask "what's x, a key or a value?" No 
> of course you wouldn't, because the question is nonsense for sets.

And also because the semantic of 'remove' is already defined for lists.

> The 
> same goes for "x in aset", "aset.add(x)" and any other set method that 
> takes an element as an argument. Why are you singling out del for this 
> pretend confusion?
>> In dicts it's a key, in sets, shouldn't it
>> also be a key and not a value?
> That question isn't even meaningful. Sets have elements. The semantics of 
> set elements is closer to dictionary keys than to dictionary values:
> x in adict  # is key x in dictionary?

This is a pragmatic design choice - just like the fact that default 
iteration over dicts is made on keys, not values - due to the most 
frequent use case. FWIW, this has been debated quite a few times here.

> x in aset  # is element x in set?

> and an early implementation of Python sets used dictionaries,

And this is only an implementation detail.

> where the 
> elements where stored as keys, not values. It makes no sense to treat set 
> elements as if they were analogous to dict values.

Would it make sense to treat them as if they where list values ?-)

>> Sets have no keys, only values. One might suggest assignment of keys, à
>> la array indexes, but that means assigning an ordering to the values,
>> whereas sets are explicitly unordered.
> Why on earth should "del aset[x]" imply that sets are ordered? Dicts 
> aren't ordered. You don't find people going "wibble wibble wibble, I 
> don't understand del adict[x] because dicts aren't ordered, what could it 
> mean???"

s/ordered/indexed/, and it makes perfect sense. Dicts and lists are 
indexed, sets are not.

More information about the Python-list mailing list