Possible to use more than one argument on __getitem__?

Viewed 19365

I am trying to use

__getitem__(self, x, y):

on my Matrix class, but it seems to me it doesn't work (I still don't know very well to use python). I'm calling it like this:

print matrix[0,0]

Is it possible at all to use more than one argument? Thanks. Maybe I can use only one argument but pass it as a tuple?

4 Answers

I learned today that you can pass double index to your object that implements getitem, as the following snippet illustrates:

class MyClass:
    def __init__(self):
        self.data = [[1]]
    def __getitem__(self, index):
        return self.data[index]
    
c = MyClass()
print(c[0][0])
Related