Equivalent code to the bool() built-in function

John Nagle nagle at animats.com
Mon Apr 18 15:58:03 EDT 2011


On 4/17/2011 5:12 PM, Gregory Ewing wrote:
> Chris Angelico wrote:
>
>> Well, of course you can always implement bool as an int;
>
> Which Python used to do once upon a time -- and still does
> in a way, because bool is a subclass of int.
>
> The bool type was added mainly to provide a type that prints
> out as 'True' or 'False' rather than 1 or 0. This can be
> a considerable help for debugging and keeping the conceptual
> meaning of one's data clear.

    This is typical for languages which backed into a "bool" type,
rather than having one designed in.  The usual result is a boolean
type with numerical semantics, like

 >>> True + True
2

which ought to either generate a TypeError or be interpreted
as "or", but due to the legacy botch, does not.

    Pascal got this right.  (A nice feature of Pascal
was that "packed array of boolean" was a bit array).
C, which originally lacked a "bool" type, got it wrong.
So did Python.  Java is in the middle, with an isolated
"boolean" type but a system that allows casts.
				
				John Nagle



More information about the Python-list mailing list