Snapshottable re-iterable iterators

Erik Max Francis max at
Tue Jun 17 22:40:00 CEST 2003

Beni Cherniavsky wrote:

> From time to time I wanted to iterate the same iterator more than once
> - but destructive iterators don't allow this.  So I wrote a class for
> wrapping destructive iterators.  It gives you an iterable, whose
> __iter__ goes over the values of the underlying iterator - but those
> are only requested once.


> So the question is: is it a good idea to violate the iterator protocol
> in this way - being an iterator but returning a "copy" of self from
> `__iter__` rather than self?  On one hand, it seems cute.  On the
> other, it makes it hard to avoid the forking when you don't want it.
> If I'll go with another method rather than `__iter__`, the best
> alternatives seem to be `copy` and `fork`.

__iter__ is a confusing name, since __iter__ is the name of the internal
method which returns an iterator.

fork and copy also seem ill-advised to me, since both have different
meanings, even in Python (re: os.fork, copy.copy).  I would think reiter
would be fine.

   Erik Max Francis && max at &&
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ Punctuality is the virtue of the bored.
\__/  Evelyn Waugh

More information about the Python-list mailing list