[Python-Dev] Re: PEP 285: Adding a bool type
Guido van Rossum
Tue, 02 Apr 2002 16:45:10 -0500
> I haven't followed this thread for quite some time, but since the
> PEP still seems alive,
> let me add some experience I've had with
> using the already existing singletons (Py_True and Py_False)
> in Python for recognizing truth values.
> Py_True and Py_False can be exposed in Python via
> True = (1==1)
> False = (1!=1)
Yuck. What a horrible implementation detail.
> and most comparison operations and quite a few other APIs
> returning truth values make use of these singletons.
This works purely by accident. Given that at least Py_False is also
known as &_Py_ZeroStruct, I'm surprised that the code that optimizes
ints doesn't initialize small_ints[NSMALLNEGINTS] with Py_False.
> I thought it would be a good idea to use those two
> singletons for protocols which use booleans such as
> XML-RPC. My experiences with this approach are, well,
> not so good :-/
> The reason is that True and False are really integers
> and not of a special new type. Now they are singletons,
> which is good, since they represent the only states
> a boolean can have and in many cases work reasonably
> well as boolean representative, but not always
> (e.g. arithmetic operations such as True - True == False).
> Also, when trying to recognize the two singletons you have
> to use the "is" comparison -- "==" will fail to
> differentiate between 1 and True...
> def isboolean(x):
> return x in (True, False)
> ...doesn't work...
> def isboolean(x):
> return (x is True) or (x is False)
The correct way if PEP 285 is accepted would of course be
> As a conclusion, I think it would be better to make bool() a
> new type which does *not* inherit from integers, but which
> does know how deal with other types which are commonly
> used together with booleans such as integers. However, the
> type should implement boolean algebra and not try to
> mimic integer arithemtic, i.e. True - True raises an
That would break more code. E.g. (a!=0) + (b!=0) + (c!=0) counts how
many of (a, b, c) are nonzero; this would break.
> Py_True and Py_False should then be made singletons
> of this new type (possibly after a transition phase which
> replaces the singletons with a version that doesn't raise
> exceptions but instead issues warnings).
That's what the PEP does (but with a different type).
> This may sound painful at first, but in the long run,
> I believe, it'll result in the same benefits as other
> painful changes have or will (e.g. the change from integer
> division to floating point division).
I don't see the introduction of the bool type as painful at all.
I find getting it accepted painful though. :-(
--Guido van Rossum (home page: http://www.python.org/~guido/)