Booleans (was Re: My .02 on PEP308 + an extra question)

Erik Max Francis max at alcyone.com
Sun Mar 2 16:36:35 EST 2003


Magnus Lie Hetland wrote:

> How are these advantages related to Python, if at all? Certainly the
> second is not, unless you are using type checking (which you rarely
> should). Maybe the first is relevant -- I can't see exactly how...

Python is a different language.  In the case of Python, the addition of
Booleans as a unique type that simply express intent ("this value is
going to be either true or false") stands on its own.  If you're looking
through a program and see a return True, you know exactly what that part
of the program is doing and what it's communicating to another part of
the program.  With Booleans, you'd see return 1, which wouldn't
communicate the information as easily (is it any positive integer? any
non-negative integer? a tristate value like that returned by cmp? or a
Boolean?  You can't tell from context).  Booleans in Python just assist
with writing self-documenting code, which is always a benefit.  Because
of Python's history of backward compatibility, getting them into the
language means they have to be degenerate integers; otherwise, too much
code would break.  This is a common compromise in languages which are
not created with a distinct Boolean type, and the objections to it are
purely theoretical (it would be "better" to have a pure Boolean type
that needs to be explicitly converted whenever encountered, but you
simply can't have that if backward compatibility is important). 
Furthermore, people are _already_ using Booleans by definining
true/false, True/False, or TRUE/FALSE, so standardizing the spelling by
making it part of the standard library has a positive benefit (the first
point Andrew made).

And yes, you _can_ overload on the Boolean type in Python (second
point), although it isn't considereed good style to explicitly overload
by type in Python.  So yes, both arguments that worked for C++ still
apply in some sense to Python, but they weren't the primary factors.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ Mona Lisa / Come to discover / I am your daughter
\__/ Lamya
    Fauxident / http://www.alcyone.com/pyos/fauxident/
 A "faux" ident daemon in Python.




More information about the Python-list mailing list