True = False == True [duplicate]
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?
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
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 as1 + 0
, which is1
, which isTrue
.str(True + False) + str(False + False)
is the same asstr(1) + str(0)
, and the+
here concatenates strings, so you'll get10
Your last one is a bunch of arithmetic operations that give a non-zero result (1), which is True.
精彩评论