Why does the comparison return an integer instead of a boolean?

I tested a piece of code

True = 2 print 1 - (1 == 1) print True == (1 == 1) 

Then I expected:

 -1 True 

But instead, I got this:

 0 False 

So, I did what any python programmer could do: parse it.

 def f(): True = 2 print 1 - (1 == 1) print True == (1 == 1) 2 0 LOAD_CONST 1 (2) 3 STORE_FAST 0 (True) 3 6 LOAD_CONST 2 (1) 9 LOAD_CONST 2 (1) 12 LOAD_CONST 2 (1) 15 COMPARE_OP 2 (==) 18 BINARY_SUBTRACT 19 PRINT_ITEM 20 PRINT_NEWLINE 4 21 LOAD_FAST 0 (True) 24 LOAD_CONST 2 (1) 27 LOAD_CONST 2 (1) 30 COMPARE_OP 2 (==) 33 COMPARE_OP 2 (==) 36 PRINT_ITEM 37 PRINT_NEWLINE 38 LOAD_CONST 0 (None) 41 RETURN_VALUE 

Then it was a little clear, uses COMPARE_OP (==) . The witch should return a boolean, but it seems that instead it returns an integer. Any ideas why?

Edit:

In short, a lesson learned: Changing the values ​​of True or False does not change the order of presentation of the logical logic behind the scene.

+4
source share
5 answers

It seems to me that your misunderstanding was that calling True as a variable would actually change the results of Boolean operations. This is not true. True used by default to represent, well, logical truth, but it loses this functionality when you change its value. However, this does not change the rules for how Booleans are processed relative to integers.

+3
source

In Python, bool is a subclass of int , while False and True have equivalent values ​​of 0 and 1 respectively. In addition, __eq__() can return any object that he likes; it so happens that for built-in types it returns bool instances.

+3
source

I think Ashvini Chaudhary commented on this issue, this is the key to understanding why things don't work the way you expect.

Python Boolean operators usually return bool(1) or bool(0) , not True or False . In Python 2, True and False are just names tied to bool(1) and bool(0) respectively. If you retype the name True to another value (for example, 2 ), it does not change the return value from the comparisons, which remains bool(1) .

In Python 3, this problem will be bypassed by making the names True and False keywords so that they cannot bounce to new values.

+3
source
 >>> True = 2 

Here you assign 2 to True . So, now True in the module area is actually 2 :

 >>> print(True) 2 

1 == 1 True . True is 1 .

 >>> 1 - (1 == 1) 0 

You may ask why this is not 2 , as indicated above. Well, the True variable is equal to region 2 in the scope of the module, and (1==1) returns a link (tag) to real True . So, 1==1 is real True , equal to 1 , so 1 - (1 == 1) is 0 .


 >>> print True == (1 == 1) False 

Here 1 == 1 again return the link to the real True . But True in the first part of the expression refers to the scope of the module, so in fact it is 2 . So this expression is effectively 2 == (True) , which is False .

+2
source

When you are minus and an integer from a boolean, its integer representation is used; since they are subclasses of int .

int(True) is 1 , so you have 1-1 , which is 0 .

In addition, -1 is a Boolean True (its not a "Falsey" value), so expecting that the answer would also not be exact:

 >>> -1 == True False >>> -1 == False False >>> bool(-1) True 
+1
source

All Articles