Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Looking at the documentation I suppose it's True == True and True != False, but I'd like a more general and certain approach. How do I quickly get readable form of bytecode for such code. Is there an easier way to find out than both bytecode and documentation?

like image 897
dhill Avatar asked Dec 04 '22 09:12

dhill


2 Answers

It's called operator chaining. Whenever you have an expression like A op1 B op2 C with op1 and op2 comparisons it is "translated" to A op1 B and B op2 C. (Actually it does evaluate B only once).

Note: comparisons operator include 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 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 at the bytecode you can understand that it performs operator chaining.

Given that the expression is True == True != False, which is "interpreted" as True == True and True != False it first loads the two True constants for the first operator via the LOAD_CONST bytecode. The 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 it just to the bytecode 21, otherwise it pops the top of the stack(JUMP_IF_FALSE_OR_POP). It then performs the second comparison.

To answer your general question the fastest way to find out about some feature of python is to use the quicksearch page of the documentation. I'd also suggest to read Python's tutorial for a general introduction to the language.

I'd like to add that, since python provides an interactive environment, it is often easier to understand how some code works writing it in the interpreter and watching the results. Almost all buil-in types have the documentation available via docstrings, so doing help(some_object) should give you a lot of information. In particular IPython provides an enhanced interactive interpreter with more user-friendly help messages/error formatting etc.)

like image 54
Bakuriu Avatar answered Dec 06 '22 21:12

Bakuriu


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

Python has a different meaning, as can be witnessed here:

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

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

Coincidentally, both meanings (Python's and that of other languages) give the same result here, but one should be cautious.

like image 42
nickie Avatar answered Dec 06 '22 21:12

nickie