[Python-ideas] Generator unpacking

Andrew Barnert abarnert at yahoo.com
Mon Feb 15 03:24:17 EST 2016


On Feb 14, 2016, at 23:30, Georg Brandl <g.brandl at gmx.net> wrote:
> 
>> On 02/15/2016 07:31 AM, Andrew Barnert via Python-ideas wrote:
>> On Feb 14, 2016, at 10:57, Georg Brandl
>> <g.brandl at gmx.net> wrote:
> 
>>> But thinking about it, this is also legal at the moment:
>>> 
>>> [] = []
>> 
>> Yes, but that's completely different. The [] on the left isn't an expression,
>> or even a target, but a target list with 0 targets in it. Assignment to
>> target lists is defined recursively, so assigning to 0 targets is legal iff
>> you're unpacking 0 values.
>> 
>> The fact that you have specifically [] on the right side is irrelevant. You
>> can get the same effect by writing [] = (), or [] = {}, or [] = (i for i in
>> range(5) if i<0). And clearly, you're not assigning to "the empty list",
>> because each empty list created with [] is a distinct object.
> 
> Yes, what you're saying can be expressed as "of course it's legal, since it's
> legal".

No; common sense isn't the same thing as tautology. It has an obvious semantics, there's no good reason to ban it, and it comes for free with the simplest grammar--therefore, it's common sense that the language should allow it. It's only because Python generally does such a great job following common sense that you don't notice. :)

And it's similar common sense that your version of "..." should make "... = ..." legal, while Nick's version should make it illegal.

> Please step back a bit and look at this from the "how it looks" perspective,
> not from the "what the grammar rules" perspective.  My observation was simple
> that we already have the case of
> 
>    X = X
> 
> where both Xes are the same syntax, but have a wildly differing interpretation.

x = x" is a no-op (assuming x was already bound) and "False = False" is an error. Given that, and the fact that (unlike those cases) "[] = []" _doesn't_ have the same syntax for the two Xes, I don't see what the observation demonstrates.


More information about the Python-ideas mailing list