[SciPy-dev] Arrays as truth values?

Robert Kern rkern at ucsd.edu
Tue Nov 8 09:46:14 CST 2005

Ed Schofield wrote:
> On Tue, 8 Nov 2005, Robert Kern wrote:
>>In any case, you can't have those methods "call .any() and .all() for
>>us;" there really is an ambiguity. We don't know beforehand which one
>>you want called. And in the face of ambiguity, we're refusing the
>>temptation to guess.
> We've seen that there's an ambiguity in the case of logical operations on
> arrays like "a and b", "a or b".  But I don't see any ambiguity in the
> case of == and !=.  Can two arrays be considered 'equal' if any of their
> elements differ? ;)

== as an operation doesn't test the equality of the arrays as a whole.
It returns an array with the results of the == comparison
element-by-element. We don't want it to return a single truth value. If
you need more semantics on top of that, then use .any() or .all() or
whatever else.

We lobbied extensively for Python to allow ==, !=, <, >, etc. to be able
to return non-Boolean values. We got that capability in Python 2.1.
We're not going to change that decision now years after the fact. If you
have a concern about code breakage, regressing now would break a huge
amount of code, much more than disabling .__nonzero__().

> You're right that my code that now raises an exception was silently wrong
> before.  That's a definite step forward.

I hoped that would have been dispositive.

>>Any API compatibility with the stdlib's array module is entirely
>>coincidental. It's not a goal and never will be.
> Yes, it is a goal.  The recent conversion of typecodes for better
> compatibility with 'array' and 'struct' is an example.  When there's no
> good argument to differ we might as well be consistent with the standard
> library.

Numeric exists because the API of stdlib's array module was inadequate
for our purposes. We've rationalized the type characters primarily to
match Python as a whole, not array specifically. Conventions about data
are a different thing than object behavior.

Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter

More information about the Scipy-dev mailing list