[Numpy-discussion] possible bug: __array_wrap__ is not called during arithmetic operations in some cases

Pierre GM pgmdevlist at gmail.com
Sun Feb 22 18:28:56 EST 2009


On Feb 22, 2009, at 6:21 PM, Eric Firing wrote:

> Darren Dale wrote:
>> Does anyone know why __array_wrap__ is not called for subclasses  
>> during
>> arithmetic operations where an iterable like a list or tuple  
>> appears to
>> the right of the subclass? When I do "mine*[1,2,3]", array_wrap is  
>> not
>> called and I get an ndarray instead of a MyArray. "[1,2,3]*mine" is
>> fine, as is "mine*array([1,2,3])". I see the same issue with  
>> division,
>
> The masked array subclass does not show this behavior:

Because MaskedArray.__mul__ and others are redefined.

Darren, you can fix your problem by redefining MyArray.__mul__ as:

     def __mul__(self, other):
         return np.ndarray.__mul__(self, np.asanyarray(other))

forcing the second term to be a ndarray (or a subclass of). You can do  
the same thing for the other functions (__add__, __radd__, ...)



More information about the NumPy-Discussion mailing list