[Tutor] True and 1 [was Re: use of the newer dict types]

Alan Gauld alan.gauld at btinternet.com
Sat Jul 27 10:26:25 CEST 2013


On 27/07/13 05:41, Steven D'Aprano wrote:

>> It's only a happy coincidence that bool(1) returns True. So does
>> bool(-9) after all...
> It's more than just a happy coincidence. True actually equals 1.
>
> For backwards compatibility, True == 1 and False == 0 had to remain the
> case. So the bool type is actually a subclass of int, but restricted to
> only two values. The side effect of this is you can do this:
>
> True + 2
> => returns 3

Most of what you said I knew, but I didn't realize it went so far as 
allowing arithmetic! Now I'm on the side of that being a bad thing.

Booleans should be as distinct from numbers as chars
are from strings... ;-)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list