
Hi all! What is the problem with iterttols.tee even? It is almots the samething - although much more flexible, than what is proposed on this thread.
from itertools import tee my_iter, peek = tee(iter(range(3)), 2) next(peek) 0 next(my_iter) 0
js -><- On 25 February 2013 09:03, Terry Reedy <tjreedy@udel.edu> wrote:
On 2/25/2013 5:08 AM, Paul Moore wrote:
On 25 February 2013 09:51, Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> wrote:
Terry Reedy <tjreedy@...> 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).
This is a bug in this re-write. The corrected .__next__
def __next__(self): if self:
ret = self.peek self._set_peek() return ret else: raise StopIteration()
passes the test with this addition
try: next(it) assert False, "Next should have raised StopIteration" except StopIteration: pass
list(lookahead('abc')) == list('abc')
True
-- Terry Jan Reedy
_______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas