Is there a boolean(somestring) equivalent of int(somestring). bool('false') -> True

Vineet Jain vineet at eswap.com
Sun Apr 11 15:52:29 EDT 2004


In other words if any case of the following strings are encountered:
'yes' 'true', 'on', '1' or True(of boolean type) True is returned
and
'no' 'false' 'off' '0' or False(of boolean type) False is returned

I end up doing the following in my code to suppr

if type(download) == type(False):
    download = download
else:
    if download.lower() == 'true' or download.lower() == 'on' or download ==
'1':
        download = True
    elif download.lower() == 'false' or download.lower() == 'off' or
download == '0':
        download = False
    else:
        raise ValueError

This surprised me:

>>> bool('false')
True
>>> bool('off')
True
>>> bool('on')
True
>>> bool('1')
True
>>> bool('0')
True
>>> bool(1)
True
>>> bool(0)
False
>>> bool(on)

there is no reason bool('false') True. If you can argue that it does/should
not interpret strings then it should raise a value error. Am I missing
something?

VJ






More information about the Python-list mailing list