[Numpy-discussion] Preventing an ndarray subclass from returning new subclass instances for std(), etc

Pierre GM pgmdevlist at gmail.com
Mon Sep 19 11:06:08 EDT 2011

> Anyway, back on topic - I'm having similar problems as Keith. It seems like there isn't consistency on how different built-in functions treat array_wrap/finalize/etc, or maybe I'm still confused.

Actually, that depends on the np function you need. Functions like np.std… first call the method of your class, and if it fails, call _method._std on your object. The call on __array_finalize__ shows up implicitly in the second case, I think, because of some recent (?) changes.
It used to be that the basic std function accepted only ndarrays. Now, _methods._std accepts any kind of subclass (it uses np.asanyarray)… So, np.std on a subclass that does not implement its std will return an instance of the subclass.
That's quite silly, IMHO. Why use asanyarray in that case ? If I wanted my subclass to return an instance of itself with std, I'd implement std as a method.  
Anyhow, Wes, in the current state of things, my understanding is that yes, you have to implement a std method like you suggest. I'd throw a copy=False, subok=False in np.array just in case, to make sure you work with a view of the underlying ndarray.

More information about the NumPy-Discussion mailing list