Python中的不等式和括号 [英] Inequalities and Parenthesis in Python

查看:177
本文介绍了Python中的不等式和括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此在python中,可以轻松检查真值条件,并通过括号将优先级确定为真条件的顺序,例如这些很容易理解:

So in python, truth conditions can be easily checked and with parenthesis it prioritize the order of true conditions, e.g. these are easy to understand:

>>> 3 > 2
True
>>> (3 > 2) is True
True

但是这些是什么意思,我无法理解为什么它们返回False/True的逻辑:

But what does these mean, I couldn't grasp the logic of why they return False/True:

>>> 3 > 2 is True
False
>>> 3 > (2 is True)
True
>>> 5 < 3 is False > 2 is True
False
>>> 5 < 3 is False is True > 2 is True
False
>>> 3 < 5 is True is True > 2 is True
False
>>> 3 < 5 is True is True > 2 is True is not False is True
False
>>> 3 < 5 is True is (True > 2 is True is not False) is True
False
>>> 3 < 5 is True is (True > (2 is True) is not False) is True
False
>>> (3 < 5 is True is True) > 2 is (True is not False is True)
False

我知道这些不是pythonic条件,但我应该如何理解它们?还是从左到右吗?

I know these are not pythonic conditions but how should I understand them? Is it still from left to right?

还是is True或/和is False担任主席?

Or does is True or/and is False takes presidence?

推荐答案

您可以使用dis模块分析每种情况,以准确了解正在发生的情况.例如:

You can analyse each of those cases with dis module to figure out exactly what's happening. For example:

In [1]: import dis
In [2]: def test():
   ...:     return 3 > 2 is True
   ...: 
In [3]: dis.dis(test)
  2           0 LOAD_CONST               1 (3)
              3 LOAD_CONST               2 (2)
              6 DUP_TOP             
              7 ROT_THREE           
              8 COMPARE_OP               4 (>)
             11 JUMP_IF_FALSE_OR_POP    21
             14 LOAD_GLOBAL              0 (True)
             17 COMPARE_OP               8 (is)
             20 RETURN_VALUE        
        >>   21 ROT_TWO             
             22 POP_TOP             
             23 RETURN_VALUE

这意味着每个步骤之后的堆栈如下所示:

That means the stack looks like this after each step:

 0: 3
 3: 3 2
 6: 3 2 2
 7: 2 3 2
 8: 2 True
11: 2
14: 2 True
17: False (comparison was: "2 is True")
20: (False is returned)

对我而言,老实说,它看起来像是Python中的错误.也许对于为什么会发生这种现象有一些很好的解释,但是我会向上游报告.

只需用等效的方式重写它,代码即可:

Just to rewrite it in equivalent way, the code does:

if 3 > 2:
    if 2 is True:
        return True
return False

也许实际上它具有某种怪异的意义.考虑一下检查连锁不平等的工作原理:

Maybe it actually makes some weird kind of sense. Consider how checking chained inequalities works:

3 > 2 > 1  ==  3 > 2 and 2 > 1

如果概括为:

x op1 y op2 z == x op1 y and y op2 z

这将解释结果.

Edit2:这实际上与文档匹配.看一下链式比较: https://docs.python.org/2/reference/expressions.html#not-in

This actually matches documentation. Have a look at chained comparisons: https://docs.python.org/2/reference/expressions.html#not-in

comparison    ::=  or_expr ( comp_operator or_expr )*
comp_operator ::=  "<" | ">" | "==" | ">=" | "<=" | "<>" | "!="
                   | "is" ["not"] | ["not"] "in"

is被认为与>一样好,因此适用于多个比较的标准扩展.

is is considered just as good comparison as >, so the standard expansion for multiple comparisons is applied.

现在应该清楚其他比较.唯一需要的新奇特细节是:True == 1False == 0,因此3 > (2 is True)中的3 > False.其他大多数可以通过扩展来解释.例如:

Other comparisons should be clear now. The only strange new detail needed is: True == 1, False == 0, so 3 > False in 3 > (2 is True). Most others can be explained with the expansions. For example:

5  <     3     is       False       >     2     is True  == False
(5 < 3) and (3 is False) and (False > 2) and (2 is True) == False

这篇关于Python中的不等式和括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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