An iterator with look-ahead

Steven Bethard steven.bethard at gmail.com
Wed Jan 10 12:10:51 EST 2007


Neil Cerutti wrote:
> For use in a hand-coded parser I wrote the following simple
> iterator with look-ahead.

There's a recipe for this:

   http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/304373

Note that the recipe efficiently supports an arbitrary look-ahead, not 
just a single item.

> I haven't thought too deeply about what peek ought to return
> when the iterator is exhausted. Suggestions are respectfully
> requested.

In the recipe, StopIteration is still raised on a peek() operation that 
tries to look past the end of the iterator.

STeVe



More information about the Python-list mailing list