[Python-Dev] Re: PEP 285: Adding a bool type

M.-A. Lemburg mal@lemburg.com
Tue, 02 Apr 2002 20:49:41 +0200

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)

and most comparison operations and quite a few other APIs
returning truth values make use of these singletons.

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)


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

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

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

Marc-Andre Lemburg
CEO eGenix.com Software GmbH
Company & Consulting:                           http://www.egenix.com/
Python Software:                   http://www.egenix.com/files/python/