Let us for simplicity just copy the diagnostic ndarray subclass from the numpy docs:
import numpy as np
class MySubClass(np.ndarray):
def __new__(cls, input_array, info=None):
obj = np.asarray(input_array).view(cls)
obj.info = info
return obj
def __array_finalize__(self, obj):
print('In __array_finalize__:')
print(' self is %s' % repr(self))
print(' obj is %s' % repr(obj))
if obj is None: return
self.info = getattr(obj, 'info', None)
Now let's do a simple example:
>>> x = MySubClass(np.ones((1,5)))
In __array_finalize__:
self is MySubClass([[1., 1., 1., 1., 1.]])
obj is array([[1., 1., 1., 1., 1.]])
>>> y = x.T
In __array_finalize__:
self is MySubClass([[1., 1., 1., 1., 1.]])
obj is MySubClass([[1., 1., 1., 1., 1.]])
As we can see something that clearly isn't the transpose is passed to __array_finalize__. Apart from stretching the meaning of the word "finalize" to entire new realms, what is the purpose of this behavior?
Wouldn't it make more sense to send the actual output, i.e. the transpose through this hook for it to be finalized?
What is the recommended way to touch up the base transpose with whatever postprocessing my subclass might require?