Python augmenting multiple variables inline

后端 未结 2 1505
Happy的楠姐
Happy的楠姐 2020-11-27 21:31

Why does this work

>> x, y = (1, 2)
>> print x, y
1 2

But augmenting results in syntax errors..

>> x, y -         


        
2条回答
  •  再見小時候
    2020-11-27 22:07

    This x, y = (1, 2) is sequence assignment. It relies on the right hand side being an iterable object, and the left hand side being composed of the same number of variables as iterating the left hand side provides.

    This x, y -= (1, 2) is attempt to call the method __isub__ on the left hand operand(s). The nature of an in-place ("augmented") assignment is that it must take a variable on its left hand side, whose value receives the operator call, and the variable then receives the result of that call. Python does not allow distribution of an in-place assignment over multiple targets.

提交回复
热议问题