[Python-ideas] Programming recommendations (PEP 8) and boolean values

Terry Reedy tjreedy at udel.edu
Thu Aug 9 23:07:02 CEST 2012

On 8/9/2012 10:58 AM, rurpy at yahoo.com wrote:

> I realize that one purpose of style conventions are
> to avoid mistakes, including beginner one's but I
> and several times found the simplest, most intuitive
> way to implement tri-value logic was to use {True,
> False, None} and so think that if "is True" is to be
> officially discouraged because misuse by some people
> in the first .5% of their Python careers, the reason
> for that discouragement should at least be explained
> in PEP-8.

To repeat what I said at the beginning of the thread.
    if x is True:
is redundant and unnecessary and wasteful EXCEPT in those rare cases 
where one specifically wants to know that x *is* True AND x could be 
another true value, so that 'is True' is necessary to differentiate True 
from other true values.

Tri-value logic with {True, False, None} is not such an exceptional case 
for True. 'if tri_value:' is equivalent to 'if tri_value is True:'. If 
you prefer the latter anyway, then ignore PEP 8 on that point.

The same principle applies to False:
   if x is False:
is equivalent to
   if not x:
EXCEPT when one wants to know if x is False AND it could be another 
false value. The same tri-value logic is such an exception for False and 
one would need 'x is False' to deal with False before None. Of course, 
one *could* always write

if x: Trueproc()
elif x is None: Noneproc()
else: Falseproc()

Terry Jan Reedy

More information about the Python-ideas mailing list