On Sun, Jun 19, 2022 at 01:34:35AM +0100, Rob Cliffe via Python-ideas wrote:
> To me, the natural implementation of slicing on a non-reusable iterator
> (such as a generator) would be that you are not allowed to go backwards
> or even stand still:
> mygen[42]
> mygen[42]
> ValueError: Element 42 of iterator has already been used
How does a generic iterator, including generators, know whether or not
item 42 has already been seen?
islice for generators is really just a thin wrapper around an iterator
that operates something vaguely like this:
for i in range(start):
next(iterator) # throw the result away
for i in range(start, end):
yield next(iterator)
It doesn't need to keep track of the last index seen, it just blindly
advances through the iterator, with some short-cuts for the sake of
efficiency.
--
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/A74YRQJ4QID72GE5I2A3QKOU6NHLJNCD/
Code of Conduct: http://python.org/psf/codeofconduct/