It seems like this is all an occasion to use itertools.tee() ... But with a
consciousness that implicit caching uses memory.

On Mon, Jun 20, 2022, 11:36 PM Steven D'Aprano <st...@pearwood.info> wrote:

> 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 -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/A74YRQJ4QID72GE5I2A3QKOU6NHLJNCD/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/KC7ATLYOWPGCG4N3A65NJ43ZWNLPR4UE/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to