python作业是从右到左严格评估的吗? [英] Is python assignment strictly evaluated right to left?

查看:106
本文介绍了python作业是从右到左严格评估的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

换句话说就是

d = {}
d["key"] = len(d)

在Python中安全吗?

safe in Python?

我知道这是之前计算?

I know this is undefined behaviour in C++; where the program might get a reference to the element before computing the value it's going to assign to it. Is this similar in Python or is len(d) always computed before d.__getitem__("key")?

推荐答案

是的,在Python中是安全的: expression 的求值顺序是从左到右,但在中赋值语句在赋值发生之前先进行评估.另外,还会按照后缀的算术顺序对算术表达式求值.

Yes, in Python it is safe: the evaluation order of an expression is from left to right, but in an assignment statement the right side is evaluated before the assignment happens. Also an arithmetic expression is evaluated in the arithmetic order of their suffixes.

5.14.评估顺序

Python从左到右计算表达式.注意,虽然 在评估作业时,在评估作业之前先评估右侧 左侧.

Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

在以下各行中,表达式将在 后缀的算术顺序:

In the following lines, expressions will be evaluated in the arithmetic order of their suffixes:

这篇关于python作业是从右到左严格评估的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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