On Mon, Nov 27, 2017 at 3:55 PM, Steven D'Aprano <steve@pearwood.info> wrote:
On Mon, Nov 27, 2017 at 12:17:31PM +0300, Kirill Balunov wrote:
​​

> 2. Should this work only for generators or for any iterators?

I don't understand why you are even considering singling out *only*
generators. A generator is a particular implementation of an iterator. I
can write:

def gen():
   yield 1; yield 2; yield 3

it = gen()

or I can write:

it = iter([1, 2, 3])

and the behaviour of `it` should be identical.



I can see where this is coming from, but I wrote about it in a new thread: "generator vs iterator etc. (was: How assignment should work with generators?)".
 

But maybe we should consider allowing slice notation on arbitrary
iterators:

x, y = it[:2]


I have not thought this through in any serious detail, but it seems to
me that if the only problem here is the inconvenience of using islice(),
we could add slicing to iterators. I think that would be better than
having iterators and other iterables behave differently.


Making iterators behave like sequences (slicing etc.) introduces various issues including memory considerations and backwards compatibility. That's why the `views` package [1] keeps a clear separations between sequences and iterators. IterABLES are a bit fuzzy here, but they at least should be able to produce an iterator. I should have time to discuss this more at a later point, if needed.

​—Koos​




--
+ Koos Zevenhoven + http://twitter.com/k7hoven +