PEP 285: Adding a bool type

Bengt Richter bokr at oz.net
Mon Apr 1 21:22:12 EST 2002


On Mon, 01 Apr 2002 15:58:32 -0800, David Eppstein <eppstein at ics.uci.edu> wrote:

>In article <a8an7b$eme$0 at 216.39.172.122>, bokr at oz.net (Bengt Richter) 
>wrote:
>
>> Well, for types that are related in certain ways ... ;-)
>> 
>>  >>> 0 == 0.0
>>  1
>>  >>> 1 == 1.0
>>  1
>>  >>> 1 == 2.0
>>  0
>> 
>> That gets into what relational operators really should mean.
>> I think I exclude bool from the number tree, so I don't
>> like the effect of subtyping bool from int, even though
>> I support context-dependent implicit conversions to integer
>> (though that context should be defined by the client expression ops).
>
>If you called it "binary" instead of "bool" would it help you think of 
>it as being more like a number?
>
No. Because numbers are ordered, and bools are not. bools are selected
elements from an unordered set of two possibilities. The bool is one chosen
element, but the element can be anything that can be distinguished from the
other element of the set. They have names str(b) associated with them, but
they don't have an order. One is chosen to mean logical true and the other
logical false, but they are just tokens with no necessary order.

By putting the bool elements in a conventional order, you can extract a
number from the situation, i.e., the position of the element in the
(now) ordered set. I.e., 0 or 1 one comes from the position of the False or True
object in a conventionally ordered set which as python list would be [False,True].

Logically the numeric value of x's boolean value is [False,True].index(bool(x))
though I'd hate to have to write it that way ;-)

False and True refer to members of a set. The members are not identical, so
the pair actually permits a binary distinction without reference to order.

You can also have sets whose elements are numbers, and ordered, and [0,1]
is a choice with optimization possibilities ;-)

But bool could just as well be represented using any two distinguishable elements,
e.g., [123, 'x'] where False=123 and True='x', and you'd still get your desired
number by [False,True].index(bool(x)), just that now bool(x) would return 123 or 'x'
instead of Guido's singletons False and True. Note that if you used False=0 and True=1,
you could fool yourself into thinking that e.g., True = [False,True].index(bool(True))
was so in general, which it is not.

(Of course, literally using 123 and 'x' wouldn't give the right str,repr, etc.,
but maybe you could subtype 123 .__class__ and 'x'.__class__ to rig two singleton
objects that might do the job, if it was reeeaally important ;-) The point is that
they don't have to have anything to do with 0 and 1 in themselves as objects, and
they don't have an inherent order, whereas numbers 0 and 1 do have counting order.

That's what I think is the relationship between bools and ints, and why they're different.
A number/set theorist might correct my terminology, but I hope
my meaning is clear.

Regards,
Bengt Richter



More information about the Python-list mailing list