Question About Logic In Python

Bengt Richter bokr at oz.net
Thu Sep 22 19:51:16 EDT 2005


On Thu, 22 Sep 2005 14:12:52 -0400, "Terry Reedy" <tjreedy at udel.edu> wrote:

>
>"Steve Holden" <steve at holdenweb.com> wrote in message 
>news:dgtu7d$o7d$1 at sea.gmane.org...
>> Which is yet another reason why it makes absolutely no sense to apply
>> arithmetic operations to Boolean values.
>
>Except for counting the number of true values.  This and other legitimate 
>uses of False/True as 0/1 (indexing, for instance) were explicitly 
>considered as *features* of the current design when it was entered.  The 
>design was not merely based on backwards compatibility, but also on 
>actually use cases which Guido did not want to disable.  There was lots of 
>discussion on c.l.p.
>
OTOH ISTM choosing to define bool as a subclass of int was a case of
practicality beats purity, but I'm not sure it wasn't an overeager coercion in disguise.

I.e., IMO a boolean is really not an integer, but it does belong to an ordered set,
or enumeration, that has a very useful correspondence to the enumeration of binary digits,
which in turn corresponds to the beginning subset of the ordered set of the non-negative integers.
IIRC Pascal actually does use an enumeration to define its bools, and you get the integer values
via an ord function.

BTW, for counting you could always use sum(1 for x in boolables if x) ;-)

And giving the bool type an __int__ method of its own might have covered a lot of coercions
of convenience, especially if __getitem__ of list and tuple would do coercion (you
could argue about coercing floats, etc. in that context, as was probably done ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list