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

Paul Moore p.f.moore at gmail.com
Mon Feb 25 11:08:42 CET 2013


On 25 February 2013 09:51, Wolfgang Maier
<wolfgang.maier at biologie.uni-freiburg.de> wrote:
> Terry Reedy <tjreedy at ...> writes:
>
>> class lookahead():
>>      "Wrap iterator with lookahead to both peek and test exhausted"
> ...
>
> +1 That's a nice tool that I'd love to have in itertools.

It's not a bad idea, but I don't like the fact that as written it
turns a finite iterator into an infinite one (returning an endless
sequence of sentinels after  the underlying iterator is exhausted).
That seems to me to be very prone to errors if you don't keep it very
clear whether you're dealing with iterators or lookahead-wrapped
iterators:

>>> from lookahead import lookahead
>>> it = lookahead(range(3))
>>> list(it)
<hangs>

The problem is that once you wrap an iterator with lookahead() you
can't use the underlying iterator directly any more without losing
data, as you've consumed a value from it. So you have to use the
wrapped version, and the differing behaviour means you need to write
your code differently.

I'd prefer it if lookahead(it) behaved exactly like it, except that it
had a new peek() method for getting the lookahead value, and maybe a
finished() method to tell if next() will raise StopIteration or not.
Bikeshed away over what should happen if peek() is called when
finished() is true :-)

(Disclaimer: I have no real-world use cases for this feature, so the
comments above are largely theoretical objections...)
Paul.



More information about the Python-ideas mailing list