defining the behavior of zip(it, it)

rurpy at yahoo.com rurpy at yahoo.com
Thu Nov 24 17:43:39 EST 2005


Alex Martelli wrote:
> <rurpy at yahoo.com> wrote:
>    ...
> > cookbook recipies of which there are already several good
> > collections, but shorter things like, copy(sequence) is spelled
> > "sequence[:]".
>
> No way:
>
> >>> from collections import deque
> >>> d=deque([1,2,3])
> >>> d[:]
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: sequence index must be integer
> >>> deque(d)
> deque([1, 2, 3])
> >>>
>
> I.e., NOT all sequences implement the unreadable x[:] form.
>
> The way that DOES work with all sequences is typeyouwant(sequence).

I'm sorry, I don't understand.  Does deque return a sequence?
The doc says it returns a deque object, and without further info
I would not expect [:] to work.  Is it a sequence because it has
the same methods as a sequence?

Whatever, I gather my old book is outdated and the blessed
way now to (shallow) copy a sequence is (as you say)
"typeyouwant(sequence)" which I will make a note of.

Thanks for updating me.




More information about the Python-list mailing list