Question about idioms for clearing a list

Tom Anderson twic at urchin.earth.li
Tue Feb 7 16:08:52 EST 2006


On Tue, 7 Feb 2006, Ben Sizer wrote:

> 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.

I don't agree with that at all. I'd been programming python for a while (a 
year?) before i knew about del l[:].

>> 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.

Ditto.

> 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.

Agreed.

tom

-- 
GOLDIE LOOKIN' CHAIN [...] will ultimately make all other forms of music
both redundant and unnecessary -- ntk



More information about the Python-list mailing list