# Explicit comparison to singleton can be unreadable

This code insight count one violation each time a condition use an explicit comparison to “none“, “false” or “true” (using operators == or !=).

if number == None: print(“This works, but is not the preferred PEP 8 pattern”)

if number ! = None : print(“This works, but is not the preferred PEP 8 pattern”)

if otherNumber == false
print(“yooo !”)

if otherNumber != false
print(“yooo !”)

if T_flag == True :
print ( “This works, but is not the preferred PEP 8 pattern” )

if F_flag ! = True :
print ( “This works, but is not the preferred PEP 8 pattern” )

good:

if number is None : print ( “This works, but is not the preferred PEP 8 pattern” )

if not otherNumber
print ( “yooo !” )

#Â or better :
if not otherNumber and otherNumber is not None

print ( “yooo !” )

if not otherNumber
print ( “yooo !” )

if Â T_ flag :

print ( “PEP 8 Style Guide prefers this pattern” )

#Â or moire restrictive check (exclude assimiled True values like non nul or non empty values)

Â  if Â T_ flag is True :

print ( “PEP 8 Style Guide prefers this pattern” )

if Â  not F_ flag :

print ( “PEP 8 Style Guide prefers this pattern” )

## Why you should care

Per the PEP 8 Style Guide, the preferred way to compare something toÂ  ` None ` Â is the patternÂ  ` ifÂ CondÂ isÂ None ` . This is only a guideline. It can be ignored if needed. But the purpose of the PEP 8 style guidelines is to improve the readability of code.

Python evaluates certain values asÂ `false`Â when in a boolean context. A quick “rule of thumb” is that all “empty” values are consideredÂ `false`Â soÂ `0, None, [], {}, ''`Â all evaluate asÂ `false`Â in a boolean context. So comparing to “false” may be confusing.

Moreover, conditions using Python booleans are easier to read and less error-prone. In most cases, they’re also faster

Per the PEP 8 Style Guide, the preferred ways to compare something toÂ  ` True ` Â are the patternsÂ  ` ifÂ condÂ isÂ True: ` Â or ` ifÂ cond: ` . This is only a guideline. It can be ignored if needed. But the purpose of the PEP 8 Style Guide is to improve the readability of code.

It is recommended to avoid these in order to ensure the code is more readable and cost effective.