True inconsistency in Python

Erik Max Francis max at alcyone.com
Mon Nov 17 20:24:54 CET 2003


Ron Adam wrote:

> To me the usefulness of using True and False is that it is defined to
> values consistent with the programming language that you are using.

I agree, insofar as the value of True and False as constants is that now
have values set aside which mean _nothing_ but their Boolean values. 
This is very helpful for writing self-documenting code, something which
I've always been a strong supporter of.  If I have code where 0 or 1
gets assigned to a variable, I'm going to have to look at the whole
block to tell precisely what that's being used for:  Is it a counter
variable, a three-state value (say, -1, 0, and +1), is it an enumerated
value type, or is it a Boolean?  If I'm scanning code and I see

	x = True

then I know right away that what I'm looking at is a variable used as a
Boolean.  (Of course, in proper self-documenting code, the variable
would be named something more helpful than `x', but you see my point.)

> So using them to assign x = True,  or x = False.  insures that when I
> do:
> 
>         x = True
>         if x:
>                 ....
> 
> So I get consistent results for the language and platform I'm using
> now and in the future.  If down the road someone decided to make True
> = 3, and False = -5,  and they change the language so that any boolean
> comparisons return 3 and -5 respectively,  my use of True and False
> will still work.  If I tested for 1 or 0 using the 'if x:' method,
> then my program will break.

While this may be true in some sense, I don't think this is a powerful
argument for using True/False.  The chances of this happening are
utterly remote, since it would break practically all code.  (Obviously
you weren't suggesting it was a real possibility given your choices of
the alternate constants, but still.)

I don't consider insulation from something utterly implausible happening
very persuasive, since I'm not worried about that utterly implausible
thing happening.  I find explicit Boolean constants available for
writing self-documenting code much more compelling.

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