[Numpy-discussion] Should bool_ subclass int?
tim.hochberg at ieee.org
Sat Jul 7 11:34:19 EDT 2007
On 7/6/07, Travis Oliphant <oliphant.travis at ieee.org> wrote:
> Timothy Hochberg wrote:
> > I'm working on getting some old code working with numpy and I noticed
> > that bool_ is not a subclass of int. Given that python's bool
> > subclasses into and that the other scalar types are subclasses of
> > their respective counterparts it seems at first glance that
> > numpy.bool_ should subclass python's bool, which in turn subclasses
> > int. Or am I missing something here?
> The reason it is not, is because it is not binary compatible with
> Python's integer. The numpy bool_ is always only 8-bits while the
> Python integer is 32-bits or 64-bits.
> This could be changed I suspect, but then it would break the
> relationship between scalars and their array counterparts
Do you have and idea off the top of your head head how painful this would be
from an implementation standpoint. And is there a theoretical reason that it
is important that the scalar and array implementations match? I would think
that, conceptually, they are all 1-bit integers, and it seems that the
8-bit, versus 32- or 64-bits is just an implementation detail.
My case is not particularly pressing or important, but I have a feeling that
this is going to bite other people eventually. In particular, if you pull a
value out of a boolean array and pass it to some third party module that
doesn't know about numpy. If that function is doing some sort of check on
argument type, which while not common does happen, then it will fail.
The workaround is straightforward of course, simply apply bool to scalars
when you get them back if you're going to be passing them to a finicky
function. That's kind of clunky and surprising though.
and I'm sure
> we would not want to bump up all bool arrays to 32 or 64-bits.
No. I wouldn't think so.
. tim.hochberg at ieee.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion