[SciPy-dev] Arrays as truth values?

Perry Greenfield perry at stsci.edu
Tue Nov 8 10:35:42 EST 2005


On Nov 8, 2005, at 9:21 AM, Ed Schofield wrote:
>
> I agree with this reasoning, but I'd like to illustrate a drawback to 
> the
> new behaviour:
>
>>>> a1 = array([1,2,3])
>>>> a2 = a1
>>>> if a1 == a2:
> ...     print "equal"
> ...

What about:

 >>> a1 = array([[1,2,3],[1,2,3]])
 >>> a2 = array([1,2,3])
 >>> a1 == a2

These two arrays are not the same shape but because of broadcasting 
will show to be equal. Is this what you intended? Some might, some 
might not.

Robert has already pointed out that lots of people want == to result in 
an array of booleans (most I'd argue) rather than a single boolean 
value.

And if you wanted to use the current Numeric behavior, then

 >>> array([0,0]) == array([0,1])

will not do what you wish it since there is at least one equal element, 
it is treated as true. (again reiterating Robert's point.) Your example 
illustrates exactly why allowing this behavior is dangerous. Two 
different people looking at this may expect two different results.

Perry




More information about the SciPy-Dev mailing list