[Numpy-discussion] Truth value of an array

Alexander Michael lxander.m@gmail....
Fri Apr 18 08:03:41 CDT 2008

On Fri, Apr 18, 2008 at 8:43 AM, Matthieu Brucher
<matthieu.brucher@gmail.com> wrote:
> 2008/4/18, Olivier Verdier <zelbier@gmail.com>:
> > I certainly didn't mean that "A==B" should return a boolean!!
> >
> > "A==B" should return an array of boolean as it does now. This is all
> right.
> >
> > *However* "bool(A==B)" should return a boolean, *not* raise an
> > exception. Why raise an exception? What is ambiguous about
> > "bool(A==B)"??
> >
> > This is what happens when you write "if A==B" because then
> > "bool(A==B)" is somehow triggered and bang, an exception is raised.
> >
> > As I tried to explain, the default behaviour should be that "bool(A)"
> > return "A.all()" but not an exception. Why is there an exception
> > raising at all?
> Sometimes, you want .all(), sometimes .any(). It really depends on the
> question you are asking. Even for bool(A), there is no easy answer. In some
> case I want True if some elements are true, in other cases only if all
> elements are true.
>  I would agree with you some years ago, but after using bool(A) = A.all(), I
> started noticing that it brakes my coding instead of speeding it and does
> not give me any benefit at all.

Not only might reasonable people differ on want any or all semantics,
it is also quite reasonable to want __nonzero__ to merely indicate
whether or not the ndarray has any elements in the first dimension
(i.e. len >0) like the built-in list and array. At least I think this
is reasonable as this is the semantic that I often want! But seeing
how there are multiple prior conceptions, perhaps the current behavior
is best because it forces us to make sure what we think is being
returned is in fact being returned?


More information about the Numpy-discussion mailing list