[Numpy-discussion] Array subclassing - question for Travis

Stéfan van der Walt stefan@sun.ac...
Thu Aug 28 00:15:07 CDT 2008


Hey Matthew

2008/8/27 Matthew Brett <matthew.brett@gmail.com>:
> In [148]: type(np.multiply(arr, obj)) # this is what I expected
> Out[148]: <class '__main__.A'>
>
> In [149]: type(np.multiply.outer(arr, obj)) # this is not - I expected
> class A again
> Out[149]: <type 'numpy.ndarray'>

Since both those objects have an __array_priority__ of 0.0, I guess it
just takes whichever class comes first.

> In [151]: class A(np.ndarray): __array_priority__ = 20.0 # setting
> array priority does not affect behavior
>
> In [152]: obj = arr.copy().view(A)
>
> In [153]: type(np.multiply.outer(arr, obj))
> Out[153]: <type 'numpy.ndarray'>

You should set the number lower:

In [15]: class A(np.ndarray):
   ....:     __array_priority__ = -1.0
   ....:
   ....:

In [16]: obj = arr.copy().view(A)

In [17]: np.multiply(obj, arr)
Out[17]: A([ 0,  1,  4,  9, 16])

In [18]: np.multiply(arr, obj)
Out[18]: A([ 0,  1,  4,  9, 16])

Cheers
Stéfan


More information about the Numpy-discussion mailing list