Python 中的这种赋值称为什么?a = b = 真 [英] What is this kind of assignment in Python called? a = b = True

查看:49
本文介绍了Python 中的这种赋值称为什么?a = b = 真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 元组解包 但这是什么任务在一行中有多个等号的地方调用?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 中的这种赋值称为什么?a = b = 真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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