On Mon, 19 Sep 2005 23:46:05 +1000, Steven D'Aprano <[EMAIL PROTECTED]> wrote:

>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()?
>
making an index (it's an int subclass), as in

 >>> things = None, 0, 1, 0.0, 5.0, '', 'a', [], [1], {}, {1:2}
 >>> for thing in things:
 ...    print 'if %-6r would act like if %s' % (thing, 
('False','True')[bool(thing)])
 ...
 if None   would act like if False
 if 0      would act like if False
 if 1      would act like if True
 if 0.0    would act like if False
 if 5.0    would act like if True
 if ''     would act like if False
 if 'a'    would act like if True
 if []     would act like if False
 if [1]    would act like if True
 if {}     would act like if False
 if {1: 2} would act like if True

Regards,
Bengt Richter
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to