A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2005-September/343141.html below:

Question About Logic In Python

Question About Logic In PythonBengt Richter bokr at oz.net
Mon Sep 19 18:31:05 EDT 2005
On Mon, 19 Sep 2005 23:46:05 +1000, Steven D'Aprano <steve at REMOVETHIScyber.com.au> 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

More information about the Python-list mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4