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

Chris Angelico rosuav at gmail.com
Mon Nov 27 11:05:39 EST 2017


On Tue, Nov 28, 2017 at 2:35 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> In this case, there is a small but real benefit to counting the
> assignment targets and being explicit about the number of items to
> slice. Consider an extension to this "non-consuming" unpacking that
> allowed syntax like this to pass silently:
>
>     a, b = x, y, z
>
> That ought to be a clear error, right? I would hope you don't think that
> Python should let that through. Okay, now we put x, y, z into a list,
> then unpack the list:
>
>     L = [x, y, z]
>     a, b = L
>
> That ought to still be an error, unless we explicity silence it. One way
> to do so is with an explicit slice:
>
>     a, b = L[:2]

I absolutely agree with this for the default case. That's why I am
ONLY in favour of the explicit options. So, for instance:

a, b = x, y, z # error
a, b, ... = x, y, z # valid (evaluates and ignores z)

ChrisA


More information about the Python-ideas mailing list