Iterator class to allow self-restarting generator expressions?

Mark Tolonen metolone+gmane at
Sun Mar 1 17:39:46 CET 2009

"John O'Hagan" <research at> wrote in message 
news:200903011520.29405.research at
> Inspired by some recent threads here about using classes to extend the
> behaviour of iterators, I'm trying to replace some some top-level 
> functions
> aimed at doing such things with a class.
> So far it's got a test for emptiness, a non-consuming peek-ahead method, 
> and
> an extended next() which can return slices as well as the normal mode, but
> one thing I'm having a little trouble with is getting generator 
> expressions
> to restart when exhausted. This code works for generator functions:

[snip code]

The Python help shows the Python-equivalent code (or go to the source) for 
things like itertools.islice and itertools.icycle, which sound like what you 
are re-implementing.  It looks like to handle generators icycle saves the 
items as they are generated in another list, then uses the list to generate 
successive iterations.


More information about the Python-list mailing list