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

Eric Firing efiring@hawaii....
Sun Feb 22 17:21:59 CST 2009


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:


In [3]:np.ma.arange(3) * [1,2,3]
Out[3]:
masked_array(data = [0 2 6],
              mask = False,
        fill_value = 999999)


In [4]:[1,2,3] * np.ma.arange(3)
Out[4]:
masked_array(data = [0 2 6],
              mask = False,
        fill_value = 999999)


Eric

> addition, etc. Here is a demonstration, observed with svn 6456:
> 
> import numpy as np
> 
> class MyArray(np.ndarray):
> 
>     __array_priority__ = 20
> 
>     def __new__(cls):
>         return np.asarray(1).view(cls).copy()
> 
>     def __array_wrap__(self, obj, context=None):
>         print 'array wrap:', self, obj, context
>         return obj.view(type(self))
> 
>     def __str__(self):
>         return 'MyArray(%s)'%super(MyArray,self).__str__()
> 
> mine = MyArray()
> 
> 
> print 3*mine
> print mine*3
> print [1,2,3]*mine
> print mine*[1,2,3]
> print
> print 3/mine
> print mine/3
> print [1,2,3]*mine
> print mine*[1,2,3]
> print
> print 3+mine
> print mine+3
> print [1,2,3]+mine
> print mine+[1,2,3]
> print
> print 3/mine
> print mine/3
> print [1,2,3]/mine
> print mine/[1,2,3]
> print
> print 3**mine
> print mine**3
> print [1,2,3]**mine
> print mine**[1,2,3]
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion



More information about the Numpy-discussion mailing list