bool behavior in Python 3000?
steven at REMOVE.THIS.cybersource.com.au
Wed Jul 11 07:10:08 CEST 2007
On Tue, 10 Jul 2007 17:47:47 -0600, Steven Bethard wrote:
>> I mean, really, does anyone *expect* True+True to give 2, or that
>> 2**True even works, without having learnt that Python bools are ints? I
>> doubt it.
>> And the old Python idiom for an if...then...else expression:
>> ["something", "or other"][True]
>> tends to come as a great surprise to most newbies. So I would argue
>> that bools being ints is more surprising than the opposite would be.
> I disagree. I think you'd get just as many odd stares if:
> True + True == True
Well, sure, if you're talking about people with no programming experience
whatsoever, or at least those who aren't at all familiar with the concept
of operator overloading. But we don't prohibit "foo" + "bar" because of
the existence of non-programmers.
> But I think all you're really saying is that newbies don't expect things
> like +, -, *, etc. to work with bools at all. Which I agree is probably
No, what I am saying is that True and False being integers under the hood
is a surprising implementation detail. It has no _inherent_ benefit: it
is merely a practical way to bring bools into the language while
remaining backward compatible. For Python 2.x, that was the least bad
solution to the issue "oops, we should have included a bool type".
Python 3 is allowed to break backwards compatibility, and there is no
reason I can see to keep the current hack.
More information about the Python-list