[Python-ideas] Have list/deque grow a copy() method

Josiah Carlson jcarlson at uci.edu
Tue May 15 17:11:58 CEST 2007


"Ian D. Bollinger" <ian.bollinger at gmail.com> wrote:
> It would be nice if containers had a more consistent interface. The
> standard idiom for copying a list has been L[:], but this isn't

The standard way of copying a list (or producing a list from an
arbitrary sequence) is list(L).

> polymorphic and perhaps arcane. Also, deques can only be copied with the
> copy module's copy function (or calling the __copy__ wrapper directly);

No, you can use deque(D).

> As an aside, in addition to the copy method, list is the only mutable
> container not to have a clear method. Of course, del L[:] is the
> standard idiom for doing so, but this has the same problem of not being
> polymorphic. Perhaps having both del L[:] and L.clear() would violate
> the TOOWTDI principle, but L.reverse() can also be done with L = L[::-1]
> (maybe not the best example since they have different semantics.)
> Nevertheless, one is a lot easier to read.

I'm -0 on list.clear(), but can see why people would prefer it over del
L[:] .


Regarding your main proposal of inserting copy.copy into the builtins,
I'm -1.  Using the object constructors themselves as copying/creation
procedures is generally seen to be a good practice, as it also typically
allows users to pass *any* iterable and have it end up in a format that
the recipient of the data wants.


 - Josiah




More information about the Python-ideas mailing list