问题描述
Python文档表示*和/具有相同的优先级。
我知道python中的表达式是从左到右进行评估。
Python docs say that * and / have the same precedence.
I know that expressions in python are evaluated from left to right.
我可以信赖吗,并假定j / m总是等于(j j)/ m
避免括号?
如果是这样的话我认为这对于一般具有相同优先级的运营商来说是适用的?
Can i rely in that and assume that jj/m is always equal to (jj)/mavoiding the parentheses?
If this is the case can i assume that this holds for operators with the same precedence in general?
ps:这个问题对我来说很好目的,
i来看它,而只读整数代码(像上面的例子)没有括号,
当时看起来很可疑我。
ps: The question as it is fine for my purposes,i came to it while reading integer-only code (like the above example) without parentheses,which at the time looked a lot suspicious to me.
推荐答案
是 - 具有相同优先级的不同运算符是左关联的;也就是说,两个最左边的项目将被操作,然后是结果和第三个项目,等等。
Yes - different operators with the same precedence are left-associative; that is, the two leftmost items will be operated on, then the result and the 3rd item, and so on.
一个例外是 * *
运算符:
>>> 2 ** 2 ** 3
256
此外,比较运算符( ==
,>
等等)不以关联方式表现,而是转换 x [cmp] y [cmp] z
into (x [cmp] y)和(y [cmp] z)
。
Also, comparison operators (==
, >
, et cetera) don't behave in an associative manner, but instead translate x [cmp] y [cmp] z
into (x [cmp] y) and (y [cmp] z)
.
这篇关于Python运算符优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!