Iterable Unpacking Evaluation Order

╄→尐↘猪︶ㄣ 提交于 2020-07-03 13:18:20

问题


I recently answered a question where a user was having trouble because they were appending a multi-dimensional array to another array, and it was brought to my attention in my answer that it is possible to use iterable unpacking to populate an x and y value and assign to board[x][y] on the same line.

I had expected this to throw an error as x and y had at the time not been defined, as, even in the iterable-unpacking tag it reads:

elements of an iterable are simultaneously assigned to multiple values

This can be seen as working in the following example:

>>> board = [[0, 0], [0, 0]]
>>> move = [0, 1, 2]
>>> x, y, board[x][y] = move
>>> board
[[0, 2], [0, 0]]

Which is the same as:

>>> board = [[0, 0], [0, 0]]
>>> move = [0, 1, 2]
>>> x = move[0]
>>> y = move[1]
>>> board[x][y] = move[2]
>>> board
[[0, 2], [0, 0]]

And yet when calculating the Fibonacci sequence using:

a, b = b, a + b

It doesn't evaluate as:

a = b
b = a + b

And when swapping values with:

a, b = b, a

It doesn't evaluate as:

a = b
b = a

So why does this work in the first example?


回答1:


The right side of the = is always evaluated first, in this case it is packing a tuple. That tuple is then unpacked when interpreting the left hand side. The left and right sides do not share knowledge of variables. The RHS becomes a value and then the LHS uses that value to assign to the variables (labels).

In your example the values of x and y are determined after the RHS is evaluated. The unpacking then occurs left to right, so that board[x][y] has valid indices.

Switching the order demonstrates the unpacking sequence:

>>> board[x][y], x, y = move[2], move[0], move[1]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-a984ef3168f8> in <module>()
----> 1 board[x][y], x, y = move[2], move[0], move[1]    
NameError: name 'x' is not defined


来源:https://stackoverflow.com/questions/47842352/iterable-unpacking-evaluation-order

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