I have a simple class that inherits from the NumPy n-dimensional array. I want to have two methods of the class that can change the array values of an instance of the class. One of the methods should set the array of the class instance to the values of a list data attribute of the class instance and the other of the methods should append some list values to the array of the class instance. I'm not sure how to accomplish this, but my attempt is as follows:
import numpyclass Variable(numpy.ndarray):def __new__(cls, name = "zappo"):self = numpy.asarray([]).view(cls)self._values = [1, 2, 3] # list of valuesreturn(self)def updateNumPyArrayWithValues(self):self = numpy.asarray(self._values)def appendToNumPyArray(self):self = numpy.append(self, [4, 5, 6])a = Variable()
print(a)
a.updateNumPyArrayWithValues()
print(a)
As a quick, preemptive question, would this class survive standard NumPy array operations such as the following?:
>>> v1 = np.array([23, 20, 13, 24, 25, 28, 26, 17, 18, 29])
>>> v1 = v1[v1 >= 20]
So, could I do similar things with my class and have all of its data attributes retained?