[Numpy-discussion] Array subclassing - question for Travis

Matthew Brett matthew.brett@gmail....
Thu Aug 28 01:50:52 CDT 2008

Hi Stefan,

Thanks for the kindly reply...

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

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

I think, playing more...

For np.multiply, it does not seem possible to demote the subclass
priority below that of the base ndarray type, but differences in array
priority do affect the priority of subtypes.

np.multiply.outer, always returns an ndarray type, regardless of the
subtypes it is passed.


import numpy as np

class SubClass1(np.ndarray):
class SubClass2(np.ndarray):
    __array_priority__ = -10.0
class SubClass3(np.ndarray):
    __array_priority__ = 1.0
arr = np.arange(5)
obj1 = arr.copy().view(SubClass1)
obj2 = arr.copy().view(SubClass2)
obj3 = arr.copy().view(SubClass3)

# Returns sub-type regardless of order or array priority
print type(np.multiply(obj1, arr))
print type(np.multiply(arr, obj1))
print type(np.multiply(obj2, arr))

# As expected
print type(np.multiply(obj1, obj2))
print type(np.multiply(obj1, obj3))
print type(np.multiply(obj2, obj3))

# multiply outer always returns ndarray type
print type(np.multiply.outer(obj1, obj2))
print type(np.multiply.outer(obj1, obj3))
print type(np.multiply.outer(obj2, obj3))


<class '__main__.SubClass1'>
<class '__main__.SubClass1'>
<class '__main__.SubClass2'>
<class '__main__.SubClass1'>
<class '__main__.SubClass3'>
<class '__main__.SubClass3'>
<type 'numpy.ndarray'>
<type 'numpy.ndarray'>
<type 'numpy.ndarray'>

Do you agree?

See you...


More information about the Numpy-discussion mailing list