list.clear() missing?!?

Raymond Hettinger python at rcn.com
Wed Apr 12 15:40:52 EDT 2006


[Steven Bethard]
> I think these are all good reasons for adding a clear method, but being
> that it has been so hotly contended in the past, I don't think it will
> get added without a PEP.  Anyone out there willing to take out the best
> examples from this thread and turn it into a PEP?

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

Here's a draft list of pros and cons (any changes or suggestions are
welcome):

Pros:
-----

* s.clear() is more obvious in intent

* easier to figure-out, look-up, and remember than either s[:]=[] or
del s[:]

* parallels the api for dicts, sets, and deques (increasing the
expecation that lists will too)

* the existing alternatives are a bit perlish

* the OP is shocked, SHOCKED that python got by for 16 years without
list.clear()


Cons:
-----

* makes the api fatter (there are already two ways to do it)

* expanding the api makes it more difficult to write classes that can
be polymorphically substituted for lists

* learning slices is basic to the language (this lesson shouldn't be
skipped)

* while there are valid use cases for re-using lists, the technique is
already overused for unsuccessful attempts to micro-optimize (creating
new lists is surprisingly fast)

* 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)




More information about the Python-list mailing list