[Numpy-discussion] Should bool_ subclass int?

Alan G Isaac aisaac at american.edu
Tue Jul 10 10:36:58 EDT 2007

On Mon, 9 Jul 2007, Timothy Hochberg apparently wrote: 
> x*y and x**2 are already decoupled for arrays and matrices. What if x*y was 
> simply defined to do a boolean matrix multiply when the arguments are 
> boolean matrices? 
> I don't care about this that much though, so I'll let it drop. 

So if x and y are arrays and you use `dot` you would get
a different result than turning them into matrices and
using `*`?  I'd find that pretty odd.

I'd also find it odd that equivalent element-by-element 
operations (`+`, `-`) would then return different outcomes 
for boolean arrays and boolean matrices.  (This is what 
I meant by "decoupled".)

This is just a user's perspective.
I do not pretend to see into the design issues.
However, daring to tread where I should not, I
offer two observations:
- matrices and 2-d arrays with dtype 'bool' should
give the same result for "comparable" operations
(where `dot` for arrays compares with `*` for matrices).
- it would be possible to have a new class, say `boolmat`,
that implements the expected behavior for boolen matrices
and then make matrices and arrays of dtype 'bool' behave
in the Python way (e.g., True+True is 2, yuck!).  I am 
definitely NOT advocating this (I like the current arrangement),
but it is a possibility.

Alan Isaac

PS Here is Guido's justification for bool inheriting from 
int (http://www.python.org/dev/peps/pep-0285/).  It seems 
that numpy's current behavior is closer to his "ideal world".

6) Should bool inherit from int?

    => Yes.

       In an ideal world, bool might be better implemented as a
       separate integer type that knows how to perform mixed-mode
       arithmetic.  However, inheriting bool from int eases the
       implementation enormously (in part since all C code that calls
       PyInt_Check() will continue to work -- this returns true for
       subclasses of int).  Also, I believe this is right in terms of
       substitutability: code that requires an int can be fed a bool
       and it will behave the same as 0 or 1.  Code that requires a
       bool may not work when it is given an int; for example, 3 & 4
       is 0, but both 3 and 4 are true when considered as truth

More information about the NumPy-Discussion mailing list