list.clear() missing?!?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Apr 12 19:15:22 EDT 2006


On Wed, 12 Apr 2006 12:40:52 -0700, Raymond Hettinger wrote:

> Something this small doesn't need a PEP.  I'll just send a note to
> Guido asking for a pronouncement.

Raymond, if you're genuinely trying to help get this sorted in the
fairest, simplest way possible, I hope I speak for everyone when
I say thank you, your efforts are appreciated.

But there is this:

> * the request is inane, the underlying problem is trivial, and the
> relevant idiom is fundamental (api expansions should be saved for rich
> new functionality and not become cluttered with infrequently used
> redundant entries)

Is this sort of editorialising fair, or just a way of not-so-subtly
encouraging Guido to reject the whole idea, now and forever? 

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)

Here is another Pro for your list:

A list.clear method will make deleting items from a list more OO,
consistent with almost everything else you do to lists, and less
procedural. This is especially true if clear() takes an optional index (or
two), allowing sections of the list to be cleared, not just the entire
list.


-- 
Steven.




More information about the Python-list mailing list