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

Chris Angelico rosuav at gmail.com
Mon Nov 27 08:47:45 EST 2017


On Mon, Nov 27, 2017 at 11:31 PM, Kirill Balunov
<kirillbalunov at gmail.com> wrote:
>
>> In terms of language proposals, you can't just say "don't need values
>> for"; the semantics have to be EITHER "consume and discard" OR "don't
>> consume". We already have a perfectly good way of spelling "consume
>> and discard":
>>
>> x, y, _ = iter
>
>
> You mean ( x, y, *_ = iter ) ?

Uhh, yeah, that's what I meant. Sorry. Anyhow, point is, there IS a
syntax for that, so we don't need another.

>> The proposed semantics, if I understand you correctly, are:
>>
>> try:
>>     _iter = iter(it)
>>     x = next(_iter)
>>     y = next(_iter)
>> except StopIteration:
>>     raise ValueError
>> # no "else" clause, we're done here
>
>
> Yes, "roughly" this semantics is proposed, with some assumptions on _iter =
> iter(it).
> 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.

That's the part I disagree with, but if you're the PEP author, you can
make the recommendation be anything you like. However, one very strong
piece of advice: it's easier to get a proposal accepted if the
backward compatibility section simply says "the proposed notation is a
SyntaxError in current versions of Python". Changing the semantics of
currently legal code requires that you demonstrate that the current
semantics are, in some way, faulty or buggy.

ChrisA


More information about the Python-ideas mailing list