Subclassing numpy ndarray problem

后端 未结 3 2108
清酒与你
清酒与你 2020-12-06 02:40

I would like to subclass numpy ndarray. However, I cannot change the array. Why self = ... does not change the array? Thanks.

import numpy as np         


        
3条回答
  •  情歌与酒
    2020-12-06 03:06

    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".

提交回复
热议问题