list.clear() missing?!?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Apr 14 12:10:09 EDT 2006


Raymond, I suspect we're not seeing eye to eye on this issue, but I do
appreciate you taking the time and effort. Thank you. My comments follow.

On Thu, 13 Apr 2006
09:34:46 -0700, Raymond Hettinger wrote:

> Both the pros and cons
> were quipped with abrupt perjoratives so the bullet points could be
> stated succinctly and with a bit of levity.  

Humour so often doesn't come across in text -- I didn't see the levity you
intended.


> "request is inane" --> "A generation of python programmers has found
> list clearing to be like other parts of the language that you get used
> to very quickly and do not prove to be a problem in practice.  The
> request is in the same category as others which challenge api choices
> made 16 years ago; in particular, the decision to have compact APIs
> where the named methods do not duplicate functionality provided by
> syntax using operators and keywords. The request is less of a bug
> report and more a rejection of Guido's sense of design and his
> subsequent experience using his own language."

Of course it isn't a bug report. It's a feature request.

As for Guido's sense of design, in *this particular instance* I think he
got it wrong. That's hardly a rejection of the man's overall design skills.

In any case, as the creator of Python, Guido has never seen the language
with the eyes of a Python beginner. All the more credit to him for
creating a language which is simultaneously friendly to beginners and
powerful for experts. But he isn't superhuman, his experience is not the
only experience. (True, as BDFL, his experience has the final vote, but
that's a whole different kettle of fish.)

A bit of searching on Google finds this issue coming up frequently. It
seems to me that there is a steady stream of Python programmers asking
"where's the list clear method?". Perhaps it isn't a monthly occurrence,
but it is still very common. That surely suggests that, as powerful
as slicing is, people aren't getting the connection between slicing and
emptying a list, and even when they do get it, many dislike it.

Since there is no evidence that these people are universally stupid, it
suggests strongly that the connection is too subtle. Some may protest that
the idiom is in the tutorial, that it is obvious once you know slicing and
del, but the fact is that many people aren't finding it obvious or
self-evident at all -- not even those who have read the tutorial.

Here is another possible solution: have the list.clear method be defined
this way:

def clear(self):
    print "Haven't you read the tutorial? Use del self[:] or self[:] = []"

That at least will stop the procession of people asking how to clear a
list, and Fredrik won't have to spend his time telling them to read the
tutorial.

(Hey, if it's good enough for quit and exit... *wink*)


>> 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.
> 
> Don't shoot yourself in the foot here.  If you want to advocate
> list.clear(), then you're hurting your chances by pushing for it to
> take an optional argument.  Essentially, this amounts to an
> unwillingness to use the del-keyword and to duplicate its functionality
> with a named method.

Deleting names from namespaces is conceptually distinct from deleting
components of compound objects like lists, and should be handled
differently: objects know their own contents, and can operate on
themselves, but they don't know what namespace they live in or what
name(s) they are known by. Deleting items from a list should be a case of
"object, operate on yourself", just like remove, append and so on, and
hence should be specified by an object method, not a keyword like del.

So you are right: I am uncomfortable having del do double duty to both
delete names and operate on the internals of compound objects. It feels
wrong to me. If insert were a keyword (as in "insert list[i:j], obj") it
would feel wrong to me too, and for the same reason.

I haven't been using Python for sixteen years, but at six years and
counting I'm not exactly a newbie. At first I accepted the del obj[:]
idiom implicitly -- the whole language was new to me, and it was no more
strange to me than any other Python syntax or idiom. But as I've got more
familiar with Python, the sense of two distinct concepts being
artificially forced into the same syntax has just gotten stronger.

As a pragmatist, I'd be happy to see a bare list.clear() method; that
would at least have the advantages of being easy to find with dir(list)
and accessible with help(). But as a purist, I really think it needs
optional arguments. Make of that what you will.



-- 
Steven.




More information about the Python-list mailing list