[Python-ideas] How assignment should work with generators?

Steven D'Aprano steve at pearwood.info
Mon Nov 27 19:25:34 EST 2017

On Mon, Nov 27, 2017 at 06:53:23PM +0200, Koos Zevenhoven wrote:

> Making iterators behave like sequences (slicing etc.) introduces various
> issues including memory considerations and backwards compatibility.


I'm not going to actively champion the idea of supporting slicing for 
iterators, but the idea I had was for no more than having 
iterator[start:stop:step] to do *exactly* what 
itertools.islice(iterator, start, stop, step) does now.

> 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 don't think the concept of iterable is fuzzy: they are a superset of 

To be precise, an iterable is something which supports iteration, which 
means it must either:

- support the iterator protocol with __iter__ and __next__ raising 

- or support the classic sequence protocol with __getitem__ raising 
IndexError at the end of the sequence.

I think that's the only two possibilities. That covers (all?) 
collections, sequences, lists, lazy computed sequences like range, 
iterators, generators, etc.



More information about the Python-ideas mailing list