True inconsistency in Python

Ben Finney bignose-hates-spam at and-benfinney-does-too.id.au
Mon Nov 17 20:33:36 EST 2003


On 18 Nov 2003 01:11:13 GMT, KefX wrote:
> un-attributed author wrote:
>>I don't see the value in this.  The expression in an if statement is
>>treated as a Boolean expression. [...]
>>However long the expression is, I don't see how adding `== true' at
>>the end makes it more clear.
>
> What if the expression is 37 lines long?

What of it?  Is a long expression in an 'if' statement somehow less
likely to be treated as Boolean?  What does appending ' == True' gain?
How could it not be obvious that the entire expression will be evaluated
for Boolean truth, when the expression is the argument to 'if' ?

> Fishing out the main comparison in such a monster isn't always the
> easiest thing to do.

How does "fishing out the main comparison" have anything to do with
appending ' == True' to the expression?

> "a == 0" isn't comparing against true or false, it's comparing against
> an integer.

Correct.  The comparison, though, is a Boolean expression, which will
evaluate to either True or False.  Its use in an 'if' statement will
always evaluate it this way.

> It's worth noting here that omitting the "== 0" here is
> considered bad style by many (though this is by no means universal).

Only in cases where ' == 0' is *not* a test for Boolean falsity.
Compare a variable against 0 if you want to test if its value is
numerically zero; evaluate the variable if it is supposed to contain a
Boolean true or false value.

> The camp that would have you write "== false" invariably falls in the
> same camp that would have you write "== 0", because that way the two
> would be consistent in that you always specify what's being compared
> to what.

No.  The argument to 'if' is a Boolean expression.  A test against
numeric zero is not the same as a test against Boolean false, except by
implementation coincidence.  

If the variable 'a' is conceptually containing a numeric value, then
using it as a Boolean value implies things that are not necessarily true
about the Boolean implementation.  Hence, 'if( a == 0 )' and not
'if( ( a == 0 ) == True )'.  Similarly, testing the negative case is
'if( not ( a == 0 ) )'.  No explicit comparison to True or False is
required.

If the variable 'a' is conceptually containing a Boolean value, then
using it as the argument to 'if' is consistent.  Hence, 'if( a )' and
not 'if( a == True )'.  Similarly, testing the negative case is
'if( not a )'.  No explicit comparison to True or False is required.

Once the expression *reads as a Boolean expression*, then adding further
comparisons against Boolean values helps nothing.

> The idea is consistency (I don't know how that slipped my mind in my
> original posting.)

I, too, value consistency, which is why I abhor adding ' == True' to
some Boolean expressions but not to others.  It should be added to none
of them.

You appear to want semantic consistency between types (integer and
Boolean) that *by design* aren't semantically consistent.  The 'if'
statement takes a Boolean expression as argument; to be consistent, one
passes it a Boolean expression argument, not some other type of
argument.

-- 
 \       "A celebrity is one who is known by many people he is glad he |
  `\                               doesn't know."  -- Henry L. Mencken |
_o__)                                                                  |
Ben Finney <http://bignose.squidly.org/>




More information about the Python-list mailing list