Simultaneous assignment semantics in Python

谁说胖子不能爱 提交于 2019-11-26 20:41:15

问题


Consider the following Python 3 code:

a = [-1,-1,-1]
i = 0

And now consider the following two versions of a simultaneous assignment over both a and i:

Assignment version 1:

a[i],i = i,i+1

Assignment version 2:

i,a[i] = i+1,i

I would expect these two versions of simultaneous assignments to be semantically equivalent. However, if you check the values of a and i after each one of the simultaneous assignments, you get different states:

Output for print(a,i) after assignment version 1:

[0, -1, -1] 1

Output for print(a,i) after assignment version 2:

[-1, 0, -1] 1

I am not an expert on Python's semantics, but this behaviour seems weird. I would expect both assignments to behave as assignment version 1. Moreover, if you check the following link, one would expect both assignment versions to lead to the same state:

Link to book excerpt in Google Books

Is there something I am missing regarding Python semantics for simultaneous assignments?

Note: This weird behaviour does not seem to be reproducible, for instance, when the variable a has integer type; it seems to require a to be of type list (maybe this is the case for any mutable type?).


回答1:


In this case:

i, a[i] = i + 1, i

The righthand side evaluates to a tuple (1, 0). This tuple is then unpacked to i and then a[i]. a[i] is evaluated during the unpacking, not before, so corresponds to a[1].

Since the righthand side is evaluated before any unpacking takes place, referring to a[i] on the righthand side would always be a[0] regardless of the final value of i

Here is another useless fun example for you to work out

>>> a = [0,0,0,0]
>>> i, a[i], i, a[i] = range(4)
>>> a
[1, 0, 3, 0]


来源:https://stackoverflow.com/questions/16409901/simultaneous-assignment-semantics-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!