Question about idioms for clearing a list
Steven D'Aprano
steve at REMOVEMEcyber.com.au
Mon Feb 6 20:19:23 EST 2006
Fredrik Lundh wrote:
> Peter Hansen wrote:
>
>
>>>Perhaps it is arguable that there is no need for a clear method because
>>>L[:] = [] is so easy to do. Personally, while I agree that it is easy, it
>>>is hardly intuitive or obvious, and I too would prefer an explicit clear
>>>method for mutable sequences.
>>
>>Possibly another case where "patches are welcome"...
>
>
> so we can have three ways to do the same thing? the right way to
> nuke a sequence is to do "del L[:]". this is explained in Python 101.
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. It also fails the introspection test:
neither dir(list) nor help(list) make it easy to
discover how to empty a list. In my opinion, the
primary advantage for a clear() method would be that it
is self-documenting.
--
Steven.
More information about the Python-list
mailing list