[Python-Dev] RE: cloning iterators again

Guido van Rossum guido at python.org
Mon Oct 27 09:53:24 EST 2003


> But I don't understand how it would be quite messy to take advantage
> of this in tee(), either: simply, tee() would start with the equivalent of
>     it = iter(it)
>     try: return it, copy.copy(it)
>     except TypeError:pass
> and proceed just like now if this shortcut hasn't worked -- that's all.

Right, that's what the tee() at the end of my code did, except it
checked for __copy__ explicitly, since I assume that only iterators
whose author has thought about copyability should be assumed copyable;
this means that the default copy stategy for class instances (classic
and new-style) as suspect.

tee is more and less powerful than copy; it is more powerful because
it works for any iterator, but less so because you can't continue
using the underlying iterator (any calls to its next() method will be
lost for both tee'ed copies).

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-Dev mailing list