Python overloading multiple getitems / index requests
operator-overloading, python
Solution
class Grid:
def __init__(self):
self.list = [[1,2], [3,4]]
def __getitem__(self, index):
return self.list[index]
g = Grid();
print g[0]
print g[1]
print g[0][1]
prints
[1, 2]
[3, 4]
2
Problem
I have a `Grid` class which I want to access using `myGrid[1][2]`. I know I can overload the first set of square brackets with the `__getitem__()` method, but what about the second. I thought I could achieve this by having a helper class which also implements `__getitem__` and then: ``` class Grid: def __init__(self) self.list = A TWO DIMENSIONAL LIST ... def __getitem__(self, index): return GridIndexHelper(self, index) class GridIndexHelper: def __init__(self, grid, index1): self.grid = grid self.index1 = index1 .... def __getitem__(self, index): return self.grid.list[self.index1][index] ``` This seems a little too homebrewed... What is the python way to achieve this?