Can I be sure that '' always will be considered False while anything else isn’t?
>>> if '':
... print('bah')
...
>>> if 'x':
... print('bah')
...
bah
Why or why not? What mechanism in Python defines this behavior?
If '' is evaluated as False, why do I get this result:
>>> if '' == False:
... print('bah')
...
>>>
From the documentation:
Any object can be tested for truth value, for use in an
iforwhilecondition or as operand of the Boolean operations below. The following values are considered false:NoneFalsezero of any numeric type, for example,
0,0L,0.0,0j.any empty sequence, for example,
'',(),[].any empty mapping, for example,
{}.instances of user-defined classes, if the class defines a
__nonzero__()or__len__()method, when that method returns the integer zero or bool valueFalse.All other values are considered true — so objects of many types are always true.