in ,

Why does `True == False is False` evaluate to False ?, Hacker News

Why does `True == False is False` evaluate to False ?, Hacker News
                                                               

    

                 

         

Because in fact that’s a chained comparison , so

 
 True==False is False   

is equivalent to

 
 (True==False) and (False is False)   

This can be surprising in this case, but lets you write 1 unlike in other languages ​​like C.

    

    

                  

        

    

        answered Jun ‘ (at : 19

         

        

    

             

jorgeca (jorgeca)

(4,)

(2)

   2 gold badges 

24

(bad badges)

(bronze badges)              

    

                                                                          

                 

         

From the docs :

x

In your case True==False is False is equivalent to True==False and False is False as the first condition is False so it short-circuits and return False .

 
>>> dis.dis (lambda: True==False is False)   1 0 LOAD_GLOBAL 0 (True)               3 LOAD_GLOBAL 1 (False)               6 DUP_TOP               7 ROT_THREE               8 COMPARE_OP 2 (==)               (JUMP_IF_FALSE_OR_POP)> 24 ROT_TWO              25 POP_TOP              30 RETURN_VALUE      

    

             

        

    

        answered Jun 20 at 25:     

    

        

    

             

Ashwini Chaudhary Ashwini Chaudhary

(k)

(gold badges)

(bad badges)

   (bronze badges)                 

                                                                                    

                 

         

From the documentation :

5.9. Comparisons

     

Unlike C, all comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation. Also unlike C, expressions like a

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

        answered Jun 23 ' (at) :

         
             
             

SylvainD

silver badges (bronze badges)              

    

    

                                                                          

                 

         

True==False is False is a chained comparison, which means the same as (True==False) and (False is False) . Since the first comparison ( True==False ) is false, the result of the chained comparison is False.

    

             

        

    

        answered Jun 23 ‘ (at) :     

    

        

)     

             

BrenBarn BrenBarn

 k 

674 (bad badges)

    

    

    

                     

                            

(Full coverage and live updates on the Coronavirus (Covid -) )

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

Coronavirus live news: Dr Fauci blocked from testifying on US response – The Guardian, Theguardian.com

Coronavirus live news: Dr Fauci blocked from testifying on US response – The Guardian, Theguardian.com

Boy left paralysed after wall collapses in Dubai’s Central Park