PEP 285: Adding a bool type

Laura Creighton lac at strakt.com
Sat Apr 6 13:39:41 EST 2002


Mark McEahern writes:
> [Laura Creighton]
> > [...]  I am rejecting the existence of booleans in the language
> > because it is too hard to teach how to use them properly.
> 
> How do you use them properly?
> 
> Suppose I want to validate users.  I might write something like this:
> 
> 	def isValid(user, password):
> 		if ...:
> 			return True
> 		else:
> 			return False

My pleasure, especially since this is such a cool example.  You have
just shot yourself in the foot, and do not know it.  When you have
shipped your isValid function, across the world to your 10,000 clients,
you will discover this.

Your boss comes in and says, oh, Mark, we need a change to that
isValid function.  We need to return 3 states, Valid, Invalid, and
Valid-but-You-Don't-Have-Access-Because-You-Didn't-Pay-Your-Bill.

What do you do now?  

> Using isValid, that returns one of two possible answers, is as clear and
> bright as day to me.  If later on I want a function that does something
> different, it would most definitely be named something different.  Why on
> earth would I want a function named isFoo to return one of more than two
> possible values?  (Your answer is probably that you never want a function
> named isFoo.  Hmm, I still don't see why.)

Do you now? Use Booleans when you want to return ON and OFF -- the
state of that bit in memory, of something that is constrained by
physical reality, or mathematical reality to only have 2 values, ever,
cross my heart and the world will end if this ever ceases to be the
case.  Don't artificially limit yourself to 2 values because your
vision at the time only extended to 2 values.  The one thing constant
in this world is change.   If you don't need a third value, somebody 
else will, later.  Integers are your friends.

Laura Creighton





More information about the Python-list mailing list