[Numpy-discussion] Allow == and != to raise errors

bruno Piguet bruno.piguet at gmail.com
Mon Jul 15 11:12:58 EDT 2013


2013/7/15 Frédéric Bastien <nouiz at nouiz.org>

> Just a question, should == behave like a ufunc or like python == for tuple?
>

That's what I was also wondering.

I see the advantage of consistency for newcomers.
I'm not experienced enough to see if this is a problem for numerical
practitionners Maybe they wouldn't even imagine that "==" applied to arrays
could do anything else than element-wise comparison ?

"Explicit is better than implicit" : to me,  np.equal(x, y) is more
explicit than "x == y".
But "Beautiful is better than ugly". Is np.equal(x, y) ugly ?

Bruno.



> I think that all ndarray comparision (==, !=, <=, ...) should behave the
> same. If they don't (like it was said), making them consistent is good.
> What is the minimal change to have them behave the same? From my
> understanding, it is your proposal to change == and != to behave like real
> ufunc. But I'm not sure if the minimal change is the best, for new user,
> what they will expect more? The ufunc of the python behavior?
>
> Anyway, I see the advantage to simplify the interface to something more
> consistent.
>
> Anyway, if we make all comparison behave like ufunc, there is array_equal
> as said to have the python behavior of ==, is it useful to have equivalent
> function the other comparison? Do they already exist.
>
> thanks
>
> Fred
>
>
> On Mon, Jul 15, 2013 at 10:20 AM, Nathaniel Smith <njs at pobox.com> wrote:
>
>> On Mon, Jul 15, 2013 at 2:09 PM, bruno Piguet <bruno.piguet at gmail.com>
>> wrote:
>> > Python itself doesn't raise an exception in such cases :
>> >
>> >>>> (3,4) != (2, 3, 4)
>> > True
>> >>>> (3,4) == (2, 3, 4)
>> > False
>> >
>> > Should numpy behave differently ?
>>
>> The numpy equivalent to Python's scalar "==" is called array_equal,
>> and that does indeed behave the same:
>>
>> In [5]: np.array_equal([3, 4], [2, 3, 4])
>> Out[5]: False
>>
>> But in numpy, the name "==" is shorthand for the ufunc np.equal, which
>> raises an error:
>>
>> In [8]: np.equal([3, 4], [2, 3, 4])
>> ValueError: operands could not be broadcast together with shapes (2) (3)
>>
>> -n
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion at scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130715/bddfd86c/attachment.html>


More information about the NumPy-Discussion mailing list