[Numpy-discussion] How to compare an array of arrays elementwise to None in Numpy 1.13 (was easy before)?

Robert Kern robert.kern at gmail.com
Mon Jul 17 13:57:11 EDT 2017


On Mon, Jul 17, 2017 at 10:52 AM, Eric Wieser <wieser.eric+numpy at gmail.com>
wrote:

> Here’s a hack that lets you keep using ==:
>
> class IsCompare:
>     __array_priority__ = 999999  # needed to make it work on either side of `==`
>     def __init__(self, val): self._val = val
>     def __eq__(self, other): return other is self._val
>     def __neq__(self, other): return other is not self._val
>
> a == IsCompare(None)  # a is None
> a == np.array(IsCompare(None)) # broadcasted a is None
>
>
Frankly, I'd stick with a well-named utility function. It's much more kind
to those who have to read the code (e.g. you in 6 months). :-)

-- 
Robert Kern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20170717/f4b1d11a/attachment.html>


More information about the NumPy-Discussion mailing list