Question about idioms for clearing a list

Raymond Hettinger python at rcn.com
Tue Feb 7 03:27:05 EST 2006


[Steven D'Aprano]
> > The Zen isn't "only one way to do it". If it were, we
> > wouldn't need iterators, list comps or for loops,
> > because they can all be handled with a while loop (at
> > various costs of efficiency, clarity or obviousness).
> >
> > del L[:] works, but unless you are Dutch, it fails the
> > obviousness test.

[Fredrik Lundh]
> unless you read some documentation, that is.  del on sequences
> and mappings is a pretty fundamental part of Python.  so are slicings.
>
> both are things that you're likely to need and learn long before you
> end up in situation where you need to be able to clear an aliased
> sequence.

Fred is exactly correct.  Slicing is absolutely basic to Python.
Accordingly, it gets covered right at the beginning of the tutorial
(section 3.1).  Likewise, the del keyword is fundamental -- if you
can't get, set, and del, then you need to go back to collections
school.

Also specious is the suggestion that dir(L) or help(L) is useless.  The
entries for the __delitem__ and __delslice__ methods are no more hidden
than for __getitem__ or __add__.  The help entry goes a step further
and shows the translation to del x[y] and del x[i:j].

While the sentiment behind the list.clear() suggestion is noble, it is
an exercise in futility to design a language around the presumption
that other programmers are incapable of learning the basics of the
language.

There was a pithy Tim Peters quotation to the effect that he was
unpersuaded by language proposals predicated on some hypothetical
average programmer not being smart enough to understand something that
the rest of us find to be basic.


Raymond Hettinger




More information about the Python-list mailing list