tags:

views:

198

answers:

2

Possible Duplicate:
Why can't Python handle true/false values as I expect?

False = True should raise an error in this case.

False = True
True == False
True

True + False == True?

if True +  False:
    print True
True

True Again?

if str(True + False) + str(False + False) == '10':
    print True
True

LOL

if True + False + True * (False * True ** True / True - True % True) - (True / True) ** True + True - (False ** True ** True):
    print True, 'LOL'
True LOL

why this is all True?

+8  A: 

False is just a global variable, you can assign to it. It will, however, break just about everything if you do so.

Note that this behavior has been removed in python3k

Python 3.1 (r31:73578, Jun 27 2009, 21:49:46) 
>>> False = True
  File "<stdin>", line 1
SyntaxError: assignment to keyword

also, int(False) == 0 and int(True) == 1, so you can do arbitrary arithmetic with them

cobbal
Since True and False are integers, in Python 2.x and 3.x, there is no need to convert them: `False == 0` and `True == 1`.
EOL
No, they're not. They're bools which happen to be easily castable to integers. Try "True is 1" if you don't believe me.
Just Some Guy
+7  A: 

See Why can't Python handle true/false values as I expect?, that will answer your first question. Basically you can think of:

False = True
True == False
True

as

var = True
True == var
True

(reminds me of #define TRUE FALSE // Happy debugging suckers *chuckles*)

As for the other questions, when you do arithmetic operations on True and False they get converted to 1 and 0.

  • True + False is the same as 1 + 0, which is 1, which is True.

  • str(True + False) + str(False + False) is the same as str(1) + str(0), and the + here concatenates strings, so you'll get 10

  • Your last one is a bunch of arithmetic operations that give a non-zero result (1), which is True.

NullUserException
In C, you need to write `typedef enum {TRUE, FALSE} BOOL;` to keep it subtle ;-)
dan04