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

Koos Zevenhoven k7hoven at gmail.com
Mon Nov 27 11:53:23 EST 2017

On Mon, Nov 27, 2017 at 3:55 PM, Steven D'Aprano <steve at pearwood.info>

> 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.


​[1] ​https://github.com/k7hoven/views

+ Koos Zevenhoven + http://twitter.com/k7hoven +
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171127/f79255a2/attachment.html>

More information about the Python-ideas mailing list