bool behavior in Python 3000?

Alexander Schmolck a.schmolck at gmail.com
Wed Jul 11 00:28:17 EDT 2007


Steven D'Aprano <steve at REMOVE.THIS.cybersource.com.au> writes:

> 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.

Sure, why not? It's pretty damn useful. Ever heard of things like "indicator
functions", "Iverson brackets" etc.? Mathematicians have long been using
broken and cumbersome ad hoc notations to be able to do stuff like
``(x<b)*f(x)`` or ``-1**(i==j)`` (e.g. ``-1^{\delta_ij}``).

And python is not alone in this either; take matlab:

>> true+true

ans =

     2

so certainly people coming from matlab to scipy *will* often expect True+True
== 2.

I'd claim that even if it weren't for backwards compatibility, python bools
should behave exactly as they are -- for a language that assigns a truth value
to instances of any type, this is the right behavior. 

'as



More information about the Python-list mailing list