[Numpy-discussion] Should bool_ subclass int?

Sebastian Haase haase at msg.ucsf.edu
Tue Jul 10 08:39:28 EDT 2007


On 7/10/07, Matthieu Brucher <matthieu.brucher at gmail.com> wrote:
>
> Hi,
>
> >
> >
> > > On Mon, 9 Jul 2007, Timothy Hochberg apparently wrote:
> > > > Why not simply use & and | instead of + and *?
> > >
> > > A couple reasons, none determinative.
> > > 1. numpy is right a Python is wrong it this case
> >
> >
> > I don't think I agree with this. Once you've decided to make Boolean a
> subclass of Int, then Python's behavior seems to be the most sensible. One
> could argue (and people did) about whether that was a good choice, but it's
> useful for a lot of practical applications. In any event, given that Boolean
> subclasses Int, I think the current behavior is probably for the best.
>
>
> If bool subclasses int, this does not enforce True+True=2. Never. Boolean
> operation live in the Boole algebra and that's it. It's not the case with
> integers that cannot be represented with int.
> Now, if you take the algebra point of view, which is the point here, for a
> scientific application, you have to have True+True = True.
>  Matthieu

When you talk about algebra - one might have to restrict one self to '|' and '&'
-- not use '+'  and '-'
E.g.:
True - True = False   # right !?
# but if:
True+True         = True.
# then
True+True   -False      = True -False   # ????
# here I'm already lost ... I don't think this can be done in a consistent way.

In other words:  a "+" operator  would also need a corresponding "-"
operator, and that will just look funny.  I think if you want algebra,
you should restrict yourself to "|" (or) and "&" (and)

My two cents,
Sebastian



More information about the NumPy-Discussion mailing list