PEP 285: Adding a bool type

Ken Seehof kseehof at neuralintegrator.com
Sat Apr 6 15:32:16 EST 2002


> Thank you very much for the reply.
> I just want to correct something you ascribed to me.
> 
> > I guess we'll have to agree to disagree here: I don't think that bools
> > are bad, either Python's variety or any other language's kind.  I've
> > seen your arguments, and they just don't convince me.  So be it.
> 
> I think there is a time and a place for bools --  Describing things
> that have two states that can never, ever, have 3 or more states.  
> These things are vanishingly close to non-existent, unless you are
> a mathematician in which case you may be playing with these sorts
> of things all the time.  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.

There is nothing wrong with isValid being boolean.  A user name is
either valid or it is not valid.  Whether the user has paid his bill
or not should be a separate variable.  Stuffing billing information
into the isValid function is a horrible idea, and in an existing code
base will almost certainly cause major hard-to-find problems.

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

It sounds to me like it may be difficult to teach because you are
using the wrong definition of boolean.

Bad definition: "A boolean represents exactly two possible values"
Good definition: "A boolean represents something that is or is not true"

Any name that starts with "is" strongly implies a boolean because things
either is or they ain't. :-)  If something looks boolean but ends up
containing a 2, you will have bugs, guaranteed.  Even after you patch
up the bugs, your code will be very confusing. Teaching when not to
use booleans can't possibly be any more difficult than teaching people
when not to prefix a function or variable with "is".

How about my_animal==your_animal returning a 2 if the animals are
different species but have identical noses, and a 3 if they like
each other?

- Ken Seehof






More information about the Python-list mailing list