list.clear() missing?!?

Peter Hansen peter at engcorp.com
Wed Apr 12 20:36:41 EDT 2006


Steven D'Aprano wrote:
> Convenience and obviousness are important for APIs -- that's why lists
> have pop, extend and remove methods. The only difference I can see between
> a hypothetical clear and these is that clear can be replaced with a
> one-liner, while the others need at least two, e.g. for extend:
> 
> for item in seq: 
>     L.append(item)

It's not even clear that extend needs two lines:

 >>> s = range(5)
 >>> more = list('abc')
 >>> s[:] = s + more
 >>> s
[0, 1, 2, 3, 4, 'a', 'b', 'c']

Okay, it's not obvious, but I don't think s[:]=[] is really any more 
obvious as a way to clear the list.

Clearly .extend() needs to be removed from the language as it is an 
unnecessary extension to the API using slicing, which everyone should 
already know about...

-Peter




More information about the Python-list mailing list