Re: True inconsistency in Python

From: Erik Max Francis (max_at_alcyone.com)
Date: 11/18/03


Date: Mon, 17 Nov 2003 18:35:59 -0800

KefX wrote:

> What if the expression is 37 lines long? (Can't happen? I'm LOOKING at
> such an
> example!) Fishing out the main comparison in such a monster isn't
> always the
> easiest thing to do. Of course I'm exaggerating here, since we usually
> don't
> write 37-line expressions, but still.

What if it's twenty pages long? I still don't see the benefit. If
you're looking at an expression in an if statement, you need to look at
the _whole_ expression to figure out what it's doing. I don't see how
ending it with `== True' makes it any more readable.

> Yes, all tests are boolean tests in the sense that they evaluate to a
> bool and
> the if statement then compares the result, but not all comparisons are
> boolean
> comparisons in the sense that we don't always compare to a boolean
> value. In
> other words, "a == 0" isn't comparing against true or false, it's
> comparing
> against an integer. It's worth noting here that omitting the "== 0"
> here is
> considered bad style by many (though this is by no means universal).

Well, if you leave off the `== 0', it means something else.

> 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.

Not even a little. An explicit test for an individual value is one
thing. An explicit comparison for an individual value _when there are
only two possible categories_ (true or false) is utterly pointless,
because all it does is add the opportunity to create an error.

> Of course, this
> doesn't
> mean everybody would have you write "== 0" would also have you write
> "==
> false". The idea is consistency (I don't know how that slipped my mind
> in my
> original posting.)

It's not at all consistent, that's the problem. Not to mention the
fact, as others pointed out, that not all false values are False.

> I don't see how that would logically follow just from making an
> explicit bool
> test.

Because in Python the explicit Boolean test cannot possibly be anything
other than redundant. If it is not redundant, it is probably an error.

-- 
   Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ 
\__/ So little time, so little to do.
    -- Oscar Levant


Relevant Pages

  • Re: both upseting now, Satam and Woodrow armed the constant covenants beneath steady warrant
    ... station is too explicit for Wayne to part it. ... Don't even try to widen home while you're formulating other than a ... then we mentally supplement Murad and Ayub's narrow ... She will comment about if Feyd's influence isn't consistent. ...
    (sci.crypt)
  • Re: True inconsistency in Python
    ... > operator, and go, "oh, there's no explicit comparison so it's ... > implicit straight boolean comparison". ... distinct Boolean type without implicit conversions (like your example, ... All including the explicit test there does is make other programmers ...
    (comp.lang.python)
  • Re: OT: The REAL RIddle of DO LOOPS Solved
    ... to be consistent with the "+1" convention, ... surely the 'logical' and 'consistent' thing for a programming language ... When encountering ambiguity, ... have worked if that's an allowable explicit statement for STEP. ...
    (sci.stat.math)
  • Re: Default __nonzero__ impl doesnt throw a TypeError exception
    ... Else if the object implements __len__, it has the boolean value of its length. ... behaviour conflicts with 'Explicit is better than implicit' ... From a practical POV, the default truth values of Python objects are most of the time what you practically want in a boolean context - that is, any non-None object, non-empty sequence and ... As a last note wrt/ explicitness and practicality: Implementing your proposition, one would have to either put each and every boolean ...
    (comp.lang.python)
  • Re: Easy function, please help.
    ... if lst: ... Example 1 is not explicit enough. ... The point is NEVER use an integer in place of a boolean! ...
    (comp.lang.python)