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

Darren Dale dsdale24 at gmail.com
Sun Feb 22 17:51:33 EST 2009


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, 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]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20090222/176c2158/attachment.html>


More information about the NumPy-Discussion mailing list