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

Kirill Balunov kirillbalunov at gmail.com
Mon Nov 27 07:59:38 EST 2017


2017-11-27 15:39 GMT+03:00 Paul Moore <p.f.moore at gmail.com>:

> On 27 November 2017 at 12:31, Kirill Balunov <kirillbalunov at gmail.com>
> wrote:
> > As I can see at the moment, these cases should behave differently:
> >
> >>>> x, y = [1,2,3,4]             # must raise ValueError
> >>>> x, y = iter([1,2,3,4])      # should work
> >
> > But at the same time, it violates current situation. So maybe, as you
> have
> > said we need special syntax. I will think about it.
>
> I would find this confusing. Consider where you don't have literals:
>
> def f(vals):
>     x, y = vals
>
> data = [1,2,3,4]
>
> f(data)
> data = iter(data)
> f(data)
>
> Having the two calls behave differently would be a recipe for errors
> as someone refactors the calling code.
>

I can not completely disagree with you, but we all adults here. My first
proposal was about generators only, but they are very similar to iterators
in their behavior. Whatever it was with this syntax, there will be no
difference:

def f(vals):
    x, y = vals

data = [1,2,3,4]

f(data)
data = (i for i in data)
f(data)

With kind regards, -gdg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171127/a4502598/attachment-0001.html>


More information about the Python-ideas mailing list