[Tutor] multiple assignment on one line

Lie Ryan lie.1296 at gmail.com
Sun Nov 22 19:33:33 CET 2009


Alan Gauld wrote:
> 
> "Lie Ryan" <lie.1296 at gmail.com> wrote
> 
> I was a bit surprised this doesn't work though:
>>
>> a, b += k, k
>>
> 
> What would you expect?

I expect
a, b += c, d
to be
a += c; b += d

just like
a, b = c, d
to be
a = c; b = d

> Remember that
> 
> a,b = c,d
> 
> is just tuple assignment without using parens (and parens are not part 
> of the tuple definition)
> 
> So using tuples
> 
> a,b += k,k
> 
> would unwrap as
> 
> a,b = (a,b) + (k,k)   -> (a,b,k,k)
> 
> which should give an error since we are then unpacking 4 values
> into two...
> 
> It doesn't really make sense.

That makes sense.

I agree "a, b += k, k" is rather ambiguous.

I disagree with the finer points of the explanation though, because in:
a, b += c, d
a, b is not really a tuple, but rather ltuple (as in a += b; a is the 
lvalue and be is the value). ltuple could (and probably should) have a 
different semantic for in-place assignment rather than the (current) 
non-sensical appending the ltuple with the value tuple. No?



More information about the Tutor mailing list