iterator clone

Yosifov Pavel bulg at ngs.ru
Mon Jul 14 03:25:15 EDT 2008


> `tee()` doesn't copy the iterator or its internal state but just caches
> it's results, so you can iterate over them again.  That makes only sense
> if you expect to use the two iterators in a way they don't get much out of
> sync.  If your usage pattern is "consume iterator 1 fully, and then
> re-iterate with iterator 2" `tee()` has no advantage over building a list
> of all results of the original iterator and iterate over that twice.
> `tee()` would be building this list anyway.

It's interesting and a concrete answer. Thanks a lot.

> Because it's often not possible without generating a list with all
> results, and the advantage of a low memory footprint is lost.
>
> Ciao,
>         Marc 'BlackJack' Rintsch

Seems like "monada". But I think is possible to determine when there
is a bounded external state (side-effects) or not, may be is needed
some new class-protocol for it... or something else. Or another way:
iterators may be re-iterable always, but if programmer need to point
to the extra- (external) state, he has to raise some a special
exception in __iter)) method... OK, it's only fantasies about language
design :-)

--pavel



More information about the Python-list mailing list