[Python-ideas] Add lookahead iterator (peeker) to itertools

Vinay Sajip vinay_sajip at yahoo.co.uk
Tue Feb 26 02:29:23 CET 2013


Barry Warsaw <barry at ...> writes:

> 
> On Feb 25, 2013, at 04:20 PM, Oscar Benjamin wrote:
> 
> >At times I thought I wanted the ability to query an iterator without
> >necessarily popping an element. I've generally found that I ended up
> >solving the problem in a different way. My own solution is to have a
> >pushable iterator so that after inspecting a value I can push it back
> >onto the iterator ready for a subsequent next() call.
> 
> The email package has one of these in its feedparser module, called
> BufferedSubFile.  It needs to be able to push lines of text back onto the
> stack that the normal iteration pops off.

It's also a common pattern in lexers/parsers/compilers where you need to push
back characters/tokens while doing lookahead for e.g. disambiguation.

Regards,

Vinay Sajip







More information about the Python-ideas mailing list