[Python-ideas] Allow "assigning" to Ellipse to discard values.

Rob Cliffe rob.cliffe at btinternet.com
Thu Feb 12 15:58:13 CET 2015


On 11/02/2015 20:06, Greg Ewing wrote:
> Daniel Holth wrote:
>> How about zero characters between commas? In ES6 destructing
>> assignment you'd writes something like ,,c = (1,2,3)
>
> Wouldn't play well with the syntax for 1-element tuples:
>
>    c, = stuff
>
> Is len(stuff) expected to be 1 or 2?
>
Does that matter?
In either case the intent is to evaluate stuff and assign its first 
element to c.
We could have a rule that when the left hand side of an assignment is a 
tuple that ends in a final comma then unpacking stops at the final 
comma, and it doesn't matter whether there are any more values to 
unpack.  (Perhaps multiple final commas could mean "unpack until the 
last comma then stop, so
     c,,, = stuff
would mean "raise an exception if len(stuff)<3; otherwise assign the 
first element of stuff to c".)
It could even work for infinite generators!

I think it's a neat idea, and it would be good if it could be made to work.
Admittedly it would be strictly backwards-incompatible, since
     c, = [1,2]
would assign 1 to c rather than (as now) raising ValueError.
But making something "work" instead of raising an exception is likely to 
break much less code that changing/stopping something from "working".
Rob Cliffe



More information about the Python-ideas mailing list