问题描述
我知道 元组解包 但这是什么任务在一行中有多个等号的地方调用?a la a = b = True
I know about tuple unpacking but what is this assignment called where you have multiple equals signs on a single line? a la a = b = True
它总是让我有点困惑,尤其是当 RHS 是可变的时,但我真的很难在文档中找到正确的关键字进行搜索.
It always trips me up a bit especially when the RHS is mutable, but I'm having real trouble finding the right keywords to search for in the docs.
推荐答案
这是一个任务链,用来描述它的术语是...
It's a chain of assignments and the term used to describe it is...
我只是在谷歌上搜索了一下,发现关于该主题的阅读量并不多,可能是因为大多数人觉得它使用起来非常简单(只有真正的极客想了解更多关于主题).
I just gave it a quite google run and found that there isn't that much to read on the topic, probably since most people find it very straight-forward to use (and only the true geeks would like to know more about the topic).
在前面的表达式中,计算的顺序可以看作是从最右边的 =
开始,然后向左工作,这相当于写:
In the previous expression the order of evaluation can be viewed as starting at the right-most =
and then working towards the left, which would be equivalent of writing:
b = True
a = b
以上顺序是大多数语言描述的赋值链,但python的做法不同.在 python 中,表达式被计算为下面的等价物,尽管它不会导致除前面描述的结果之外的任何其他结果.
The above order is what most language describe an assignment-chain, but python does it differently. In python the expression is evaluated as this below equivalent, though it won't result in any other result than what is previously described.
temporary_expr_result = True
a = temporary_expr_result
b = temporary_expr_result
可在 stackoverflow 上进一步阅读:
Further reading available here on stackoverflow:
- 链式作业的工作原理是什么? python
这篇关于Python 中的这种赋值称为什么?a = b = 真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!