python优先级最高的运算符,Python是==运算符优先级

In Python3,

a = b = 3

a is None == b is None

returns False, but

(a is None) == (b is None)

returns True. So I would assume based on this example alone, == has precedence over is.

However,

a = b = None

a is None == b is None

returns True. And

(a is None) == (b is None)

returns True. But

a is (None == b) is None

returns False. In this case, it would seem as if is has precedence over ==.

To give another example, and this expression isn't meant to do anything, but bear with me please. If I say

None is None == None

it returns True. But both of the following return False.

None is (None == None)

(None is None) == None

So clearly, Python isn't evaluating these with some strict precedence, but I'm confused what is going on. How is it evaluating this expression with 2 different operators, but differently from either order?

解决方案

What you see here is operator chaining and there is no precedence involved at all!

Python supports expressions like

1 < a < 3

To test that a number is in between 1 and 3; it's equal to (1 < a) and (a < 3) except that a is only evaluated once.

Unfortunately that also means that e.g.

None is None == None

actually means

(None is None) and (None == None)

which is of course True, and the longer example you started with

a = b = 3

a is None == b is None

means

(a is None) and (None == b) and (b is None)

which can only be True if both a and b are None.

Documentation here, see the bit about chaining.

Very useful sometimes but it also pops up when you least expect it!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值