可迭代解包评估顺序 [英] Iterable Unpacking Evaluation Order

查看:52
本文介绍了可迭代解包评估顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近回答了一个问题 用户遇到麻烦,因为他们将一个多维数组附加到另​​一个数组,并且在我的回答中引起了我的注意,可以使用可迭代解包来填充 xy 值并分配给同一行的 board[x][y].

我原以为这会引发错误,因为 xy 当时尚未定义,即使在 iterable-unpacking 标记为:

<块引用>

迭代的元素同时分配给多个值

这可以在以下示例中看到:

<预><代码>>>>板 = [[0, 0], [0, 0]]>>>移动 = [0, 1, 2]>>>x, y, board[x][y] = 移动>>>木板[[0, 2], [0, 0]]

与以下相同:

<预><代码>>>>板 = [[0, 0], [0, 0]]>>>移动 = [0, 1, 2]>>>x = 移动[0]>>>y = 移动 [1]>>>板[x][y] = 移动[2]>>>木板[[0, 2], [0, 0]]

然而在使用以下方法计算斐波那契数列时:

a, b = b, a + b

它不会评估为:

a = bb = a + b

当交换值时:

a, b = b, a

它不会评估为:

a = bb = a

那么为什么这在第一个示例中有效?

解决方案

= 的右侧总是首先被评估,在这种情况下它正在打包一个元组.然后在解释左侧时解压缩该元组.左侧和右侧不共享变量知识.RHS 成为一个值,然后 LHS 使用该分配给变量(标签).

在您的示例中,xy 的值是在评估 RHS 之后确定的.然后从左到右解包,以便 board[x][y] 具有有效索引.

切换顺序演示解包顺序:

<预><代码>>>>板[x][y],x,y = 移动[2],移动[0],移动[1]---------------------------------------------------------------------------NameError Traceback(最近一次调用最后一次)<ipython-input-7-a984ef3168f8>在 <module>()---->1 板[x][y], x, y = 移动[2], 移动[0], 移动[1]NameError: 名称 'x' 未定义

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?

解决方案

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

这篇关于可迭代解包评估顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆