What is the meaning of True == True! = False in Python and how to find out?

I really needed xor for my solution, but thinking about it, I started wondering about the above question. What is the meaning of True == True != False ?

Looking at the documentation, I believe that this is True == True and True != False , but I need a more general and specific approach. How to quickly get a readable form of bytecode for such code. Is there an easier way to find out than both bytecode and documentation?

+4
source share
2 answers

It is called a chain of operators . Whenever you have an expression of type A op1 B op2 C with comparisons of op1 and op2 , it "translates" to A op1 B and B op2 C (In fact, he evaluates B only once).

Note: the comparison operator includes in , not in , is , is not ! (e.g. a is b is not None means a is b and b is not None ).

If you want to look at the bytecode, you can use the dis module:

 In [1]: import dis In [2]: dis.dis(lambda: True == True != False) 1 0 LOAD_CONST 1 (True) 3 LOAD_CONST 1 (True) 6 DUP_TOP 7 ROT_THREE 8 COMPARE_OP 2 (==) 11 JUMP_IF_FALSE_OR_POP 21 14 LOAD_CONST 2 (False) 17 COMPARE_OP 3 (!=) 20 RETURN_VALUE >> 21 ROT_TWO 22 POP_TOP 23 RETURN_VALUE 

If you read in bytecode, you can understand that it is executing a chain of statements.

Given that the expression True == True != False , which is "interpreted" as True == True and True != False , it first loads two True constants for the first statement through the LOAD_CONST . DUP_TOP duplicates the top of the stack (this avoids re-evaluating True for the second comparison). It performs the first comparison ( COMPARE_OP ) if it is false only for bytecode 21, otherwise it JUMP_IF_FALSE_OR_POP top of the stack ( JUMP_IF_FALSE_OR_POP ). He then performs a second comparison.

To answer your general question, the quickest way to find out about a python function is to use the quicksearch page in the documentation. I also suggest reading a Python tutorial for a general introduction to the language.

I would like to add that since python provides an interactive environment, it is often easier to understand how any code works by writing it in the interpreter and observing the results. Almost all buil-in types have documentation available through docstrings, so help(some_object) should provide you a lot of information. In particular, IPython provides an improved interactive interpreter with more convenient help messages / error formatting, etc.)

+9
source

In most languages, a == b != c parsed as (a == b) != c Therefore, you expect True == True != False be the same as (True == True) != False , which evaluates to True != False , which evaluates to True .

Python has a different meaning, which can be seen here:

 >>> True != False != False False >>> (True != False) != False True 

In Python, a == b != c equivalent to (a == b) and (b != c) . This means that True == True != False equivalent to (True == True) and (True != False) , which evaluates to True and True , which evaluates to True .

By the way, both values ​​(Python and other languages) give the same result here, but you need to be careful.

+4
source

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


All Articles