比较运算符和“是"-python 中的运算符优先级? [英] Comparison operators and 'is' - operator precedence in python?

查看:65
本文介绍了比较运算符和“是"-python 中的运算符优先级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在网上查看了一些代码然后我来了穿过一条线(在第 286 行):
<代码>如果深度>0 并且最好 <= -MATE_VALUE 是 None 和 nullscore >-MATE_VALUE:

我难以理解的部分是最好的<= -MATE_VALUE is None.

所以我启动了解释器,看看像 value1 > 这样的语句是如何产生的.value2 是 value3 工作.
所以我尝试了

<预><代码>>>>5 >2 是真的错误的>>>(5 > 2) 为真真的>>>5 >(2 为真)真的


我的问题

为什么 5 >2 是 True 不是 True?这些东西通常如何运作?

谢谢.

解决方案

你正在看到 python 的操作符链工作

5 >2 是真的

相当于

5>2 和 2 为真

你可以看到这个

<预><代码>>>>5>2 是 2

返回 True.

So I was looking at some code online and I came across a line (at line 286):
if depth > 0 and best <= -MATE_VALUE is None and nullscore > -MATE_VALUE:

The part I had trouble understanding was the best <= -MATE_VALUE is None.

So I fired up the interpreter to see how a statement such as value1 > value2 is value3 work.
So I tried

>>> 5 > 2 is True
False

>>> (5 > 2) is True 
True

>>> 5 > (2 is True) 
True


My Question

Why is 5 > 2 is True not True? And how do these things generally work?

Thanks.

解决方案

You're seeing python's operator chaining working

5 > 2 is True

Is equivalent to

5>2 and 2 is True

You can see this in that

>>> 5>2 is 2

Returns True.

这篇关于比较运算符和“是"-python 中的运算符优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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