On Mon, May 11, 2020 at 12:50 AM Christopher Barker <python...@gmail.com> wrote:
> I'm still confused what you mean by extend to all iterators? you mean that > you could use slice syntax with anything iterable> > > And where does this fit in to the iterable vs iterator continuum? > > iterables will return an iterator when iter() is called on them. So are > you suggesting that another way to get an iterator from an iterable would > be to pass a slice somehow that would return an iterator off that slice? > > so: > > for i in an_iterable(a:b:c): > ... > > would work for any iterable? and use an iterator that would iterate as > specified by the slice? > Translate `an_iterable(a:b:c)` to `itertools.islice(an_iterable, a, b, c)`. >From there your questions can be answered by playing with itertools.islice. It accepts any iterable or iterator and returns an iterator: ``` import itertools s = itertools.islice([1, 2, 3], 2) print(s) assert s is iter(s) s2 = itertools.islice(s, 1) print(s2) ``` > Though it is heading in a different direction that where Andrew was > proposing, that this would be about making and using views on sequences, > which really wouldn't make sense for any iterator. > The idea is that islice would be the default behaviour and classes could override that to return views if they want.
_______________________________________________ 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/DTPLAE5ZQVUTQSTOBKOVPNNMHDZIEPHN/ Code of Conduct: http://python.org/psf/codeofconduct/