[Python-Dev] List copy and clear (was Re: Inconsistent API for sets.Set and build-in set)

Raymond Hettinger raymond.hettinger at verizon.net
Thu Jun 30 23:26:03 CEST 2005


[Nicolas Fleury]
> This comment reminds me another small inconsistency/annoyance.
> 
> Should copy and clear functions be added to lists, to be more
consistent
>   with dict and set, and easing generic code?

I think not.

Use copy.copy() for generic copying -- it works across a wide range of
objects.  Alternatively, use the constructor as generic way to make
duplicates:

   dup = set(s)
   dup = list(l)
   dup = dict(d)
   dup = tuple(t)     # note, the duplicate is original object here :-)

I would think that that generic clearing is a lark.  First, it only
applies to mutable objects.  Second, it would likely only be useful in
the presence of a generic method for adding to the cleared container (as
opposed to the existing append(), add(), and setitem() methods for
lists, sets, and dictionaries respectively).  So, for lists, stick with
the current idiom:

   mylist[:] = []       # clear



Raymond



More information about the Python-Dev mailing list