[Numpy-discussion] Scalar-ndarray arguments passed to not_equal

Friedrich Romstedt friedrichromstedt@gmail....
Thu Feb 11 14:29:49 CST 2010

Oh Sorry, I typed some keys, don't know what I did precisely, but the
message was sent prematurely. Now I repeat:

Hi Keith,

2010/2/11 Keith Goodman <kwgoodman@gmail.com>:
> Is there some way to tell numpy to use my __eq__ instead of its own?
> That would solve my problem. I had a similar problem with __radd__
> which was solved by setting __array_priority__ = 10. But that doesn't
> work in this case.

It's quite simple, but hidden in the forest of documentation (though
it mentions it, and quite in detail).


numpy.set_numeric_ops(equal = my_equal_callable_object)

Note that you should _not_ simply use a function:

def equal(a, b):
    if isinstance(b, myclass):
        return a == b.arr
    return numpy.equal(a, b)

This will, in many cases, work, though, but not in _all_.  This is
because Numpy sometimes calls some .reduce attribute and such things,
I'm also not completely informed what they do, but it seems to be safe
to simply hand them over to numpy's original functions (i.e., callable
objects).  I coded a callable object (an instance of a class with
__call__()), and simply download upy from
github.com/friedrichromstedt/upy, and please find the overload at the
very end of core.py.  hth.

>> I wish I knew enough to reply to your post. Then I could return the
>> favor. You'll have to settle for a thank you. Thank you.
Ah, that's great, thank you very much.  I love to help.


P.S.: I had the same problem as you, just some days ago, don't know
precisely how I got alert of set_numeric_ops, but it's very powerful.
Use it with care!  You can crash your whole numpy with it.

More information about the NumPy-Discussion mailing list