[Numpy-discussion] Bug? NumPy types circumvent some overloaded methods in ndarray subclasses
Wes McKinney
wesmckinn at gmail.com
Sat May 7 20:04:37 EDT 2011
This strikes me as a bug-- haven't checked NumPy 1.6 yet but this
happens in 1.5.1. Here's a toy example:
class MyNdarray(np.ndarray):
def __new__(cls, data):
subarr = np.array(data, dtype=np.float64).view(cls)
return subarr
def __radd__(self, other):
print 'hello'
return (self.view(np.ndarray) + other).view(MyNdarray)
def __add__(self, other):
print 'hello'
return (self.view(np.ndarray) + other).view(MyNdarray)
In [25]: foo = MyNdarray(np.arange(10.))
In [26]: foo + 3
hello
Out[26]: MyNdarray([ 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.])
In [27]: foo + np.float64(3)
hello
Out[27]: MyNdarray([ 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.])
In [28]: 3 + foo
hello
Out[28]: MyNdarray([ 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.])
In [29]: np.float64(3) + foo
Out[29]: MyNdarray([ 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.])
I'm not sure if there's an easy workaround here. I thought maybe it
was an array priority issue but after some tinkering it doesn't look
like it. Any ideas?
- Wes
More information about the NumPy-Discussion
mailing list