Question About Logic In Python

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Sep 21 00:56:11 CEST 2005


On Mon, 19 Sep 2005 22:31:05 +0000, Bengt Richter wrote:

> On Mon, 19 Sep 2005 23:46:05 +1000, Steven D'Aprano <steve at REMOVETHIScyber.com.au> wrote:

>>Are there actually any usage cases for *needing* a Boolean value? Any
>>object can be used for truth testing, eg:

[snip]

> 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)])
>  ...

That's a pretty artificial example though. Your general index ranges from
0 to n inclusive, where n is unlikely to be 1. That limits the usefulness
of the idiom sequence_or_mapping[bool(thing)] to a tiny set of cases.

As near as I can tell, explicitly converting objects to booleans is mostly
useful for demonstrating that booleans aren't needed for truth testing.


-- 
Steven.




More information about the Python-list mailing list