开发者

operator precedence: not and comparisons

开发者 https://www.devze.com 2023-04-12 07:28 出处:网络
I\'m trying to understand python better and the lack of parentheses can be a bit confusing for some reason.

I'm trying to understand python better and the lack of parentheses can be a bit confusing for some reason.

how is (not a < b < c) evaluated? Is it (not a) < b < c? or not (a < b < c)?

Acco开发者_StackOverflow社区rding to the reference manual, does not have a lower or higher precedence than < ? I'm assuming operators with higher precedence evaluate before those with lower, right? I feel like I need someone to break out the sock puppets right now.

According to the Python 2.4 reference manual not and comparisons have a different precedence. Then in the Python 2.7 reference manual not and comparisons have the same precedence.

If i'm not mistaken not a < b < c will have varying results depending on the version of python. Would someone please share how this statement is evaluated?

I'm sticking with not (a < b < c)


What you're seeing in the 2.7 manual is all relational operators, including not in and is not, at the same precedence; boolean not is still one level lower in precedence and as such the relational comparison happens first.

0

精彩评论

暂无评论...
验证码 换一张
取 消