[Numpy-discussion] Truth value of an array

Matthieu Brucher matthieu.brucher at gmail.com
Fri Apr 18 08:43:40 EDT 2008


2008/4/18, Olivier Verdier <zelbier at 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.

Matthieu
-- 
French PhD student
Website : http://matthieu-brucher.developpez.com/
Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
LinkedIn : http://www.linkedin.com/in/matthieubrucher
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20080418/75d06864/attachment.html>


More information about the NumPy-Discussion mailing list