Question About Logic In Python
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Mon Sep 19 09:46:05 EDT 2005
On Mon, 19 Sep 2005 12:16:15 +0200, sven wrote:
> to make sure that an operation yields a boolean value wrap a bool()
> around an expression.
> None, 0 and objects which's len is 0 yield False.
> so you can also do stuff like that:
Are there actually any usage cases for *needing* a Boolean value? Any
object can be used for truth testing, eg:
if the_str
is to be preferred over:
if bool(the_str)
or even worse:
if bool(the_str != "")
Or wait, I have thought of one usage case: if you are returning a value
that you know will be used only as a flag, you should convert it into a
bool. Are there any other uses for bool()?
--
Steven.
More information about the Python-list
mailing list