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?

Community
  • 1
  • 1
user422100
  • 2,099
  • 6
  • 22
  • 22
  • because these are converted to True => 1 and False => 0 in the process? Im not familiar with the Python but some languages just emulate these as constants to byte/int/(bit?). – Imre L Aug 31 '10 at 05:56
  • 4
    In Python 3, this is not possible. – carl Aug 31 '10 at 05:58
  • You can check the discussion about True == 1 at http://stackoverflow.com/questions/2764017/is-false-0-and-true-1-in-python-an-implementation-detail-or-guaranteed-by-t – Eric O. Lebigot Aug 31 '10 at 07:11

2 Answers2

12

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
  • 69,903
  • 20
  • 143
  • 156
  • 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`. – Eric O. Lebigot Aug 31 '10 at 07:09
  • 2
    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. – Kirk Strauser Aug 31 '10 at 13:36
  • 2
    @KirkStrauser: You should try `isinstance(True, int)`: it is True. I did not write that `True is 1`, only that True is an integer (through inheritance). – Eric O. Lebigot Feb 03 '13 at 03:40
7

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.

Community
  • 1
  • 1
NullUserException
  • 83,810
  • 28
  • 209
  • 234