[Python-ideas] Set Syntax

Chris Rebert pyideas at rebertia.com
Tue Nov 23 20:08:45 CET 2010


On Mon, Nov 22, 2010 at 8:02 AM, Gerald Britton
<gerald.britton at gmail.com> wrote:
<snip>
> Note that dictionaries and sets have clear() methods:
>
>>>> d = {1:2}
>>>> d
> {1: 2}
>>>> d.clear()
>>>> d
> {}
>>>> s = set([1,2])
>>>> s
> set([1, 2])
>>>> s.clear()
>>>> s
> set([])
>
> Deques have this method as well:
>
>>>> from collections import deque
>>>> q = deque([1,2])
>>>> q
> deque([1, 2])
>>>> q.clear()
>>>> q
> deque([])
>
> If lists had the clear() method, then the basic collection objects
> would be in alignment.

Possible objection: Violates TOOWTDI.

There are already 2 ways to clear a list:
del lst[:]
lst[:] = []

Do we necessarily need a third?

Unlike lists, dicts, sets, and deques don't support slice subscripts,
which is arguably the only reason they need .clear() methods.

Also, is it that common to treat such significantly differently
collection types generically?

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-ideas mailing list