Creating a subclass that never propagates

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

On Tue, Jul 16, 2019 at 3:44 AM Kevin Sheppard <kevin.k.sheppard@gmail.com> wrote:
I think you will be able to do that with `__array_function__` in the upcoming 1.17 release. It is also in 1.16, but you need an environmental variable to activate it. Some documentation can be found at https://www.numpy.org/devdocs/reference/arrays.classes.html#special-attribut... . Chuck

On Tue, Jul 16, 2019 at 5:58 AM Charles R Harris <charlesr.harris@gmail.com> wrote:
I think you need to implement __array_finalize__ for this (see e.g. https://docs.scipy.org/doc/numpy-1.13.0/user/basics.subclassing.html#implica... )
That's kind of an orthogonal thing: __array_function__ is for providing your own implementation of functions, which you don't necessarily want to do if you're just building a small subclass. Cheers, Ralf

On Tue, Jul 16, 2019 at 3:44 AM Kevin Sheppard <kevin.k.sheppard@gmail.com> wrote:
I think you will be able to do that with `__array_function__` in the upcoming 1.17 release. It is also in 1.16, but you need an environmental variable to activate it. Some documentation can be found at https://www.numpy.org/devdocs/reference/arrays.classes.html#special-attribut... . Chuck

On Tue, Jul 16, 2019 at 5:58 AM Charles R Harris <charlesr.harris@gmail.com> wrote:
I think you need to implement __array_finalize__ for this (see e.g. https://docs.scipy.org/doc/numpy-1.13.0/user/basics.subclassing.html#implica... )
That's kind of an orthogonal thing: __array_function__ is for providing your own implementation of functions, which you don't necessarily want to do if you're just building a small subclass. Cheers, Ralf
participants (3)
-
Charles R Harris
-
Kevin Sheppard
-
Ralf Gommers