Boolean comparison & PEP8

Richard Damon Richard at Damon-Family.org
Sun Jul 28 21:57:07 EDT 2019


On 7/28/19 8:46 PM, Chris Angelico wrote:
> On Mon, Jul 29, 2019 at 10:43 AM Richard Damon <Richard at damon-family.org> wrote:
>> On 7/28/19 8:25 PM, Chris Angelico wrote:
>>> Of course, if the third value can be simplified away (eg None means
>>> "use the global default"), then you can still just use "if verbose is
>>> None:" and then reassign it. But this is a legit reason to use "is
>>> False".
>>>
>>> ChrisA
>> The more common way to handle the 3 value case in my experience is to
>> first handle the 'special case' (check for None in your example) and
>> then use the normal truthiness test.
>>
> That works fine if you can actually handle the special case first and
> dispose of it, but that doesn't always work, and when it doesn't, you
> may indeed want to specify "is False" or "is True".
>
> Chris

If you can't handle it first (or really at the same time)


def foo(verbose=None):
    if verbose is not None:
        if verbose:
          # Specified Verbose case
       else:
          # Specified none Verbose case

which is really the same as before if you left the Not Specified case empty.

Again, the question is do really need to force the giving of a bool, or can you deal with a truthy/falsey/None value? (which may just require a bool call at the call site, with some code to possibly handle the None case. 

-- 
Richard Damon




More information about the Python-list mailing list