First Mystery of Strings:
How come bool('foo')
returns True
?
if
'foo' == True
returnsFalse
'foo' == False
returnsFalse
'foo' is True
returnsFalse
'foo' is False
returnsFalse
Second Mystery of Integers:
How come bool(5)
returns True
?
if
5 == True
returnsFalse
5 == False
returnsFalse
5 is True
returnsFalse
5 is False
returnsFalse
Third Mystery of Zeros:
How come bool(0)
returns False
?
if
0 == True
returnsFalse
0 == False
returnsTrue
<-- Special Case
0 is True
returnsFalse
0 is False
returnsFalse
I am aware of some of the truthiness of Python, however, this all seems a bit mysterious. Someone mind shedding some light on this?
you need to read this: https://docs.python.org/2/library/stdtypes.html#truth-value-testing
'foo' == True # -> False
'' == True # -> False
'' == False # -> False
will always be False
. a string does not equal a bool
. but - yes - bool('non-empty-str') -> True
; bool('') -> False
.
and so on for you other 'mysteries'.
is
compares the identities id()
of two objects (there are some mysteries here as well: What's with the Integer Cache inside Python?)
also this may be interesting: Is False == 0 and True == 1 in Python an implementation detail or is it guaranteed by the language?