开发者

True = False == True [duplicate]

This question already has answers here: 开发者_运维百科 Closed 12 years ago.

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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜