tags:

views:

640

answers:

2

Inspired by this question.

Why is there no list.clear() method in python? I've found several questions here that say the correct way to do it is one of the following, but no one has said why there isn't just a method for it.

del lst[:]
lst[:] = []

While it may go against the "zen of python" to have more than one way of doing something, it certainly seems more obvious to me to have a "list.clear()" method. It would also fall in line with dicts and sets, both of which have .clear().

I came across a few posts to the python-dev and python-ideas concerning this and didn't come to a definitive answer (see here (2006) and here (2009)). Has Guido weighed in on it? Is it just a point of contention that hasn't been resolved yet over the last 4-5 years?

+7  A: 

In the threads you linked Raymond Hettinger pretty much sums up the pros and cons of adding that method. When it comes to language design, it's really important to be conservative. See for example the "every feature starts with -100 points" principle the C# team has. You don't get something as clean as Python by adding features willy-nilly. Just take a look at some of the more cruftier popular scripting languages to see where it takes you.

I guess the .clear() method just never did cross the implicit -100 points rule to become something worth adding to the core language. Although given that the methodname is already used for an equivalent purpose and the alternative can be hard to find, it probably isn't all that far from it.

Ants Aasma
+1: There's no sensible use case for clearing a list when you have garbage collection that works.
S.Lott
+4  A: 

I can't answer to the why; but there absolutely should be one, so different types of objects can be cleared with the same interface.

An obvious, simple example:

def process_and_clear_requests(reqs):
    for r in reqs:
        do_req(r)
    reqs.clear()

This only requires that the object support iteration, and that it support clear(). If lists had a clear() method, this could accept a list or set equally. Instead, since sets and lists have a different API for deleting their contents, that doesn't work; you end up with an unnecessarily ugly hack, like:

def process_and_clear_requests(reqs):
    for r in reqs:
        do_req(r)
    if getattr(reqs, "clear"):
        reqs.clear()
    else:
        del reqs[:]

As far as I'm concerned, using del obj[:] or obj[:] = [] are just unpleasant, unintuitive hacks to work around the fact that list is missing clear().

This is taking "reducing redundancy" to a fault, where it damages the consistency of the language, which is even more important.

As to which you should use, I'd recommend del obj[:]. I think it's easier to implement for non-list-like objects.

Glenn Maynard
Good points about having consistent interfaces. I was going to suggest that you use pop() in your example (I know it's just an example), but `pop()` only serves to highlight interface inconsistency!
mhawke
I had removed this note for brevity, but: set's "add" and list's "append" should also have had the same name, for similar reasons. Oh well; all languages have their warts.
Glenn Maynard