On Sun, 1 Mar 2009, Mark Tolonen wrote: > "John O'Hagan" <resea...@johnohagan.com> wrote in message > news:200903011520.29405.resea...@johnohagan.com... > > > 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. >
Thanks for your reply Mark; I've looked at the itertools docs (again, this time I understood more of it!), but because the generators in question produce arbitrarily many results (which i should have mentioned), it would not always be practical to hold them all in memory. So I've used a "buffer" instance attribute in my iterator class, which only holds as many items as are required by the peek(), next() and __nonzero__() methods, in order to minimize memory use (come to think of it, I should add a clear() method as well...). But the islice() function looks very useful and could replace some code in my generator functions, as could some of the ingenious recipes at the end of the itertools chapter. It's always good to go back to the docs! As for restarting the iterators, it seems from other replies that I must use generator function calls rather than expressions in order to do that. Thanks, John -- http://mail.python.org/mailman/listinfo/python-list