[Python-Dev] bool conversion wart?

Larry Hastings larry at hastings.org
Fri Feb 23 03:19:38 CET 2007


Neal Becker wrote:
> Instead, bool fails in _the worst possible way_: it silently gives a 
> _wrong result_.

I disagree with the word "fail" there; Python is working correctly.  The 
behavior of converting expressions to a boolean is well-defined:
    http://docs.python.org/ref/Booleans.html
Perhaps the behavior is unexpected or unwelcome--but it is *correct*.  
It's just one of those things about Python that one has to get used to; 
this is a side-effect of another, much more important principle.

If you really want to turn the string "True" into True, use "eval".
    eval("4") -> 4
    eval("True") -> True
    eval("{}") -> {}
Perhaps it would be safest to cast the output of eval to bool:
    bool(eval("True")) -> True
    bool(eval("")) -> False


Cheers,

/larry/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20070222/15e6eb76/attachment-0001.html 


More information about the Python-Dev mailing list