Possible Duplicate:
Is False == 0 and True == 1 in Python an implementation detail or is it guaranteed by the language?
A brief transcript from my interactive console:
Python 2.7.2 (default, Jun 29 2011, 11:10:00)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> True
True
>>> 0 == True
False
>>> 1 == True
True
>>> 2 == True
False
Why on earth is this the case?
Edit: For the sake of contrast, consider the is
operator.
>>> 0 is False
False
>>> 1 is True
False
>>> 0 is 0
True
>>> True is True
True
That makes a lot of sense because though 1
and True
both mean the same thing as the condition of an if
statement, they really aren't the same thing.
Edit again: More fun consequences of 1 == True
:
>>> d = {}
>>> d[True] = "hello"
>>> d[1]
"hello"