del and sets proposal

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Oct 5 06:12:39 EDT 2008


On Sun, 05 Oct 2008 22:11:38 +1300, Lawrence D'Oliveiro wrote:

> In message <00f80c6a$0$20633$c3e8da3 at news.astraweb.com>, 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. 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?
x in aset  # is element x in set?

and an early implementation of Python sets used dictionaries, 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.


> 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???"


-- 
Steven




More information about the Python-list mailing list