[Numpy-discussion] Creating a subclass that never propagates

Kevin Sheppard kevin.k.sheppard at gmail.com
Tue Jul 16 05:43:31 EDT 2019


I am trying to make a subclass that never propagates so that when
interacted with another ndarray, or even itself so that the return type is
always ndarray.  Is this possible?

I got pretty far with

def __array_wrap__(self, out_arr, context=None):
    if out_arr.shape == ():
        return out_arr.item()  # if ufunc output is scalar, return it
    else:
        out = super(ArrayLike, self).__array_wrap__(out_arr, context)
        # Never return ArrayLike
        if isinstance(out, ArrayLike):
            out = out.view(np.ndarray)
        return out

Which works well for ufuncs.  However, when I try other functions like
`dot` I get my subclass type returned.

If there a reasonable way to ensure that my subclass doesn't propagate? I
think I would need some way to override the behavior when .view(MySubClass)
is called.

Thanks,
Kevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20190716/05d37545/attachment.html>


More information about the NumPy-Discussion mailing list