6

I was testing some piece of code

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

Then I was expecting:

-1
True

But I got this instead:

0
False

So, I did what any python programmer would do: disassemble 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 bit clear, is using the COMPARE_OP (==). Witch should return a boolean but it appears that it returns a integer instead. Any ideas why?

Edit:

In short the lesson learned: Changing the values of True or False doesn't change how the boolean logic is represented behind the scene.

5
  • 3
    In python 3 this has been fixed, True and False are keywords now, you can't assign anything to them. Commented Jan 15, 2013 at 6:25
  • However, 3>> True == 1 True Commented Jan 15, 2013 at 6:26
  • python.org/dev/peps/pep-0285 Commented Jan 15, 2013 at 6:33
  • 1
    As a side note, the actual lesson here is that names have nothing to do with values. Given ten=42, would you expect 9+1 to return 42? Commented Jan 15, 2013 at 10:19
  • @thg435 No, I would expect ten == (9 + 1) ;) Commented Jan 17, 2013 at 6:19

5 Answers 5

5

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

Sign up to request clarification or add additional context in comments.

Comments

3

It seems to me that your misunderstanding was in thinking that treating True like a variable would actually change the results of boolean operations. It doesn't. True is used by default to respresent, well, boolean truth, but it loses that functionality when you changes its value. However, that doesn't change the rules for how booleans are treated with respect to integers.

Comments

3

I think Ashwini Chaudhary's comment on the question is key to understanding why things are not working the way you expect.

Boolean operators in Python generally return bool(1) or bool(0), not the values of True or False. In Python 2, True and False are just names that are bound to bool(1) and bool(0) respectively. If you rebind the name True to a different value (such as 2), it doesn't change the return value from the comparisons, which remains bool(1).

In Python 3, this issue is sidestepped by making the names True and False into keywords, so they cannot be rebound to new values.

Comments

2
>>> True = 2

Here you assign 2 to True. So now True in the module's scope is actually 2:

>>> print(True)
2

1 == 1 is True. True is equal to 1.

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

You could ask why it isn't 2, as above. Well, True variable is equal to 2 in the module's scope, and (1==1) just returns the reference (tag) to real True. So 1==1 is a real True which is equal to 1, so 1 - (1 == 1) is equal to 0.


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

Here 1 == 1 again return reference to real True. But the True in the first part of the expression is from the module's scope, so it is actually 2. So this expression is effectively 2 == (True) which equals to False.

Comments

1

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

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

Also, -1 is a boolean True (its not a "Falsey" value) so expecting that as an answer would not have been accurate either:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.