Question about idioms for clearing a list

Ben Sizer kylotan at gmail.com
Tue Feb 7 05:02:42 EST 2006


Raymond Hettinger wrote:
> [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).

Yes, right after UTF encoding details, complex numbers, and various
mentions of shell scripts. I don't want to criticise the hard work that
went into making the tutorial but let's not pretend it's the epitome of
documentation or even necessary the most obvious reference for users.

> Likewise, the del keyword is fundamental -- if you
> can't get, set, and del, then you need to go back to collections
> school.

I have hardly used the del keyword in several years of coding in
Python. Why should it magically spring to mind in this occasion?
Similarly I hardly ever find myself using slices, never mind in a
mutable context.

del L[:] is not obvious, especially given the existence of clear() in
dictionaries. I'm not necessarily requesting a clear() method, but I am
requesting a little more understanding towards those who expected one.
The list interface is full of redundant convenience methods, so one
more would hardly be a surprise or an unreasonable thing for people to
expect. Again we unfortunately have a bit of an attitude problem
towards anyone posting here that doesn't know whatever the experts
think is obvious.

-- 
Ben Sizer




More information about the Python-list mailing list