You are conflating thruthness with equality; they are not the same thing. Use bool()
if you need to compare to actual True
and False
values:
>>> bool('abc')
True
You generally do not compare directly with == True
or == False
. Rather, you use conditional expressions or statements:
if 'abc':
pass
while 'def':
pass
foo if 'abc' else bar
Only empty containers, numeric zero and False
and None
are falsey, everything else is thruthy; you can use bool()
to test for that condition explicitly. You can make custom types look like empty containers by implementing __len__()
and returning 0
, or look like a number by implementing __nonzero__()
* and returning False
when the instance is to be the boolean equivalent of numeric zero.
*In Python 3, use __bool__
instead.