del and sets proposal

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Oct 2 20:02:12 EDT 2008


On Thu, 02 Oct 2008 15:39:55 -0700, Chris Rebert wrote:

> On Thu, Oct 2, 2008 at 3:20 PM, Larry Bates <larry.bates at vitalesafe.com>
> wrote:
>> a = set([1,2,3,4,5])
>> del a[1]
> 
> Sets don't support subscripting, so if you can't go 'a_set[something]',
> why would you expect to be able to be able to 'del' such an expression?
> What would the subscription even mean without the 'del'? It doesn't make
> sense and would just be inconsistent.

Then add subscription access too.  By aliasing `__getitem__()` to 
`__contains__()`.  And `__setitem__()` could be implemented to add or 
remove objects by assigning truth values.  So hypothetically:

>>> a = set([1, 2, 3])
>>> a[1]
True
>>> a[4]
False
>>> a[2] = False
>>> a
set([1, 3])
>>> a[4] = True
>>> a
set([1, 3, 4])
>>> del a[1]
>>> a
set([3, 4])

I wouldn't want that addition to `set`\s but at least it can be 
implemented without introducing inconsistencies.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list