Overloading operators in numpy

1738 views python

I have the following problem: I wish to create and use numpy array with slight change in operator []. I understand so far it is done by the method __getitem__(self, index). However I am unable to figure how to do it so I declare an array that is "numpy array" in every aspect except of that one issue (say for sake of example I want array[i] to be interpreted as array[i-1]

I tried to solve it following way:

class myarray(np.ndarray):
def __getitem__(self, index):
    return self[index+1]

k = np.linspace(0, 10, 10).view(myarray)

though it's not really working

answered question

1 Answer


I have never used numpy.

This seems like a ridiculous way of doing such a simple task O.O If this doesn't seem insane to you, I'd suggest looking into inheritance in python, as there are some large errors here. You would need an init method for starters, then overload overwrite the __getitem__ method.

I would suggest you just take one from the index to be honest.

posted this

Have an answer?


Please login first before posting an answer.