On 27/07/13 08:48, Alan Gauld wrote:
On 26/07/13 22:55, Jim Mooney wrote:

And using True in place of 1 is sneakily pedagogical ;')

It's not 'in place of' 1, it is a totally different value and type.
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.

Way back in Ancient Days, when dinosaurs walked the earth and men used Python 
1.5, there was no bool type in Python, no True, no False. Instead, 1 and 0 were 
used as the canonical boolean values. So for example:

[steve@ando ~]$ python1.5 -c "print 'c' in 'cat'"
1
[steve@ando ~]$ python1.5 -c "print 'c' in 'dog'"
0


Unfortunately, this leads to a little confusion, especially with functions like 
cmp() that return 1, 0 or -1. People got confused and thought it returned 1 for 
equal and 0 for not equal, instead of 0 for equal, 1 for greater than and -1 
for less than.

Also, people would define their own named bools in their modules:

true = 1
false = not true

etc. So eventually GvR changed his mind and added bools to Python, not without 
a *lot* of controversy:

http://www.python.org/dev/peps/pep-0285/


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

which depending on your perspective is either a terrible thing or a great thing.



--
Steven
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to