The reason you are not getting the result you expect is because you are re-assigning self
within the method remove_some
. You are just creating a new local variable self
. If your array shape were not to change, you could simply do self[:] = ... and you could keep the reference to self
and all would be well, but you are trying to change the shape of self
. Which means we need to re-allocate some new memory and change where we point when we refer to self
.
I don't know how to do this. I thought it could be achieved by __array_finalize__
or __array__
or __array_wrap__
. But everything I've tried is falling short.
Now, there's another way to go about this that doesn't subclass ndarray
. You can make a new class that keeps an attribute that is an ndarray and then override all the usual __add__
, __mul__
, etc.. Something like this:
Class Data(object):
def __init__(self, inarr):
self._array = np.array(inarr)
def remove_some(x):
self._array = self._array[x]
def __add__(self, other):
return np.add(self._array, other)
Well, you get the picture. It's a pain to override all the operators, but in the long run, I think more flexible.
You'll have to read this thoroughly to do it right. There are methods like __array_finalize__
that need to be called a the right time to do "cleanup".
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…