Boolean comparison & PEP8

Michael Torrie torriem at gmail.com
Sun Jul 28 19:46:51 EDT 2019


On 7/28/19 5:55 AM, Jonathan Moules wrote:
> But this appears to be explicitly called out as being "Worse" in PEP8:
> 
> """
> Don't compare boolean values to True or False using ==.
> 
> Yes:   if greeting:
> No:    if greeting == True:
> Worse: if greeting is True:
> """

Yet the recommended solution to the problem of wanting a default
argument of an empty list is something like this:

def foo(bar=False);
    if bar is False:
        bar = []

    ....

Clearly in this case the expression "not bar" would be incorrect.

There's a difference between looking for a particular identity or
sentinel, and checking for truthiness or falsiness.

So I guess it all depends on what you need to do. If you just need to
check truthiness, just do "if greeting"  If you need to know if the
variable is some particular sentinel, use the "if greeting is whatever".



More information about the Python-list mailing list