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

bruno Piguet bruno.piguet at gmail.com
Mon Jul 15 11:00:19 EDT 2013


Thank-you for your explanations.

So, if  the operator  "==" applied to np.arrays is a shorthand for the
ufunc np.equal, it should definitly behave exactly as np.equal(), and raise
an error.

One side question about style : In case you would like to protect a "x ==
y" test by a try/except clause, wouldn't it feel more "natural" to write "
np.equal(x, y)" ?


Bruno.


2013/7/15 Nathaniel Smith <njs at pobox.com>

> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20130715/fb979ac7/attachment.html>


More information about the NumPy-Discussion mailing list