bool behavior in Python 3000?
steve at REMOVE.THIS.cybersource.com.au
Thu Jul 12 04:45:48 CEST 2007
On Wed, 11 Jul 2007 07:14:53 -0600, Steven Bethard wrote:
> Steven D'Aprano wrote:
>> On Tue, 10 Jul 2007 17:47:47 -0600, Steven Bethard wrote:
>>> 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.
> Remember that while Python 3 is allowed to break backwards
> compatibility, it's only supposed to do it when there are concrete
> benefits. Clearly there are existing use cases for treating bools like
> ints, e.g. from Alexander Schmolck's email:
> (x < b) * f(x)
> -1 ** (i == j)
You have cause and effect confused here. Expressions like (i == j) used
to return 0 and 1, and it was to avoid breaking hacks like the above
that bools were implemented as a subclass of int, not because being
able to write the above was a specific feature requested.
In the hypothetical bright new world of Python with bools that are
actually bools, the above are good cases for explicit being better than
int(x < b) * f(x)
-1 ** int(i == j)
It makes more sense to explicitly cast bools to ints when you want to
do integer arithmetic on them, rather than explicitly casting bools to
bools to do boolean arithmetic! I feel strongly enough about this that I
believe that being able to write (x < b) * f(x) is a DISADVANTAGE -- it
gives me a real WTF moment to look at the code.
In some hypothetical world where backwards compatibility was not an
issue, where bools had already existed, if somebody had specifically asked
for bools to become ints so they could write (x < b) * f(x), I have every
confidence that their request would have been denied, and they would have
been told to explicitly cast the bool to an int. As they should.
More information about the Python-list