PEP 285: Adding a bool type

Erik Max Francis max at alcyone.com
Sat Apr 6 15:52:58 EST 2002


Laura Creighton wrote:

> What I am trying to do is save people
> like Mark McEahern from his isValid function, which returns whether
> a username is valid or not.  On the day his boss demands a third
> value - valid-but-I-won't-let-you-in-anyway-you-didn't-pay-your-bill,
> he will thank me.

I really don't see what you think the big benefit is.  A Boolean is a
logical return value for a test function; I use them all the time, and
have seen them used very often on large projects.  In my experience,
it's pretty rare that a simple yes-or-no question needs to be expanded
to a multiple choice question; in those questions, at any rate, the
introduction of another function which returns an enum (or equivalent)
is probably more appropriate.

I also don't know of many cases where bosses get down to the level of
what individual functions return; I can't imagine the hell of a
micromanaging environment where a typical supervisor is really concerned
at that level of detail.

Further, let's take the case at face value:  You have a function that
returns a bool, and you realize later in the design that you need a
three-state function -- say, "yes," "no," and "unknown."  You're saying
that the person who uses a Boolean is punished for the implementation
change, but why?  Not only does the function in question need to be
changed to return the three-state value now, but now other code which
calls it _also_ needs to be changed, totally independent of whether or
not the original formulation was a Boolean, just an integer, or
something else.

An API change means that code which uses it needs to be touched; whether
you started by returning a Boolean or not doesn't change that fact.  If
you had started by using integers (say, 0, and 1) and need to add a
third value (maybe -1 or 2), if you started by using a Python bool then
the impact will be just as minimal as if you had used 0 and 1, since
True == 1 and False == 0.  I really don't see what benefit you're
getting at.

> That is what I mean by teaching people not to use bools -- teaching
> when not to use them. This is very hard to teach.  I know it because
> I have been doing it for years and years.   I even know _why_ it is
> hard to teach, but that doesn't make it any easier.

I really don't see where teaching bools is hard, particularly
considering it's a lot more straightforward than teaching them to return
arbitrary values for different conditions.  "If a function is a
yes-or-no question, return a bool."  Is that _really_ that hard?

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Nationalism is an infantile sickness.
\__/ Albert Einstein
    Alcyone Systems' Daily Planet / http://www.alcyone.com/planet.html
 A new, virtual planet, every day.



More information about the Python-list mailing list