Inequalities and brackets in Python

Thus, in python, truth conditions can be easily checked, and in parentheses it prioritizes the order of true conditions, for example. they are easy to understand:

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

But what does this mean, I could not understand 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

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

Or is it executed is Trueand / or is False?

+4
source share
3 answers

You can analyze each of these cases with a module disto determine exactly what is going on. 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

, :

 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, . , - , , .

, :

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

: , - . , :

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

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

is , >, .

. : True == 1, False == 0, 3 > False 3 > (2 is True). . :

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

Boolean python - int. , True 1 False 0.

Python (>, <, >=, <=, ==, !=, is [not], [not] in).

, , x < y <= z x < y and y <= z, , y ( z , x < y ).

, a, b, c,..., y, z op1, op2,..., opN , a op1 b op2 c ... y opN z a op1 b and b op2 c and ... y opN z, , .

. Python.

+2

, , . . "" :

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

: ( __is__ __not__, , . )

(3.__lt__(5).__is__(True).__is__(True)).__gt__(2).__is__(True.__is__(False).__not__().__is__(True))

, . , () , , , .

EDIT: Nevermind, this does not work with comparisons. Comparisons are compared “collectively in pairs,” as described in viraptor's answer.

+1
source

Source: https://habr.com/ru/post/1617675/


All Articles