[Numpy-discussion] Overload binary operators with custom array type

Adam Jenkins adam at thejenkins.org
Fri Jan 12 14:37:57 CST 2007


Timothy Hochberg <tim.hochberg <at> ieee.org> writes:
> You want __array_priority__; add an attribute __array_priority__ to your
array-like object and set it's value to, say, 10 and ndarrays will defer to your
object.

Thank you very much.  I had seen the __array_priority__ property mentioned in
the Numpybook, but it's only documented there as being used to decide which
operand's type the result of an operation should be converted to, so I didn't
realize it would also be used to decide which operand to defer the operation to.

I just tried it, and it does solve the problem for the mathematical operators
like + and -, but doesn't seem to work for the comparison operators.  That is,
if I write

lhs < rhs

where lhs is a numpy.ndarray and rhs is a myarray, numpy still treats rhs as a
scalar and tries to compare each element of lhs to rhs, even though I set
myarray.__array_priority__.  Is this intentional that the comparison operators
ignore __array_priority__, or just a bug in ndarray?  Thanks again.

Adam



More information about the Numpy-discussion mailing list