True inconsistency in Python

Erik Max Francis max at alcyone.com
Mon Nov 17 14:18:31 EST 2003


KefX wrote:

> I'm not so sure about that. In most languages (including Python),
> 'false' is
> guaranteed to be only one value (usually zero), therefore comparing
> against it
> poses no problem. I assume you're saying that doing so isn't
> problematic in any
> way, it's just pointless.

Yes.  The same cascade into insanity developes.  If you're testing for
falsity, why not write x == false?  But what about _that_ test,
shouldn't that be (x == false) != false?  And _that_ test ...

> Well, actually, some coding standards suggest you always compare
> against false.

My point is that any such coding standard is dumb.

> But if whatever goes in "blah" is really long (and often it is), you
> very
> quickly see what is being compared: it's a straight boolean
> comparison, whereas
> with the second you have to look at the whole thing, find no
> comparison
> operator, and go, "oh, there's no explicit comparison so it's
> obviously an
> implicit straight boolean comparison".

I don't see the value in this.  The expression in an if statement is
treated as a Boolean expression.  In languages where there isn't a
distinct Boolean type without implicit conversions (like your example,
C89), it doesn't matter whether you put the explicit comparison there or
not.  However long the expression is, I don't see how adding `== true'
at the end makes it more clear.  It's a Boolean test, what's to make
explicit?

All including the explicit test there does is make other programmers
wonder if you've lost your mind, and admit the possibility of you making
the type of error we've been discussing (x == True) where True is a
solitary constant in a much larger world of Booleans which evaluate as
true.  Trying to correct that error, you end up with monstrosities such
as bool(x) == True or operator.truth(x) == True (or the equivalent in
other languages), and after some point it should dawn on the programmer
that the explicit True test is pointless.

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ 
\__/ Why don't you grow up for crying out loud?
    -- Capt. Benjamin "Hawkeye" Pierce




More information about the Python-list mailing list