Python przeciąża wiele zapytań getitem / index
mamGrid
klasa, do której chcę uzyskać dostępmyGrid[1][2]
. Wiem, że mogę przeciążyć pierwszy zestaw nawiasów kwadratowych__getitem__()
metoda, ale co z drugą.
Pomyślałem, że mogę to osiągnąć, mając klasę pomocniczą, która również implementuje__getitem__
i wtedy:
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]
Wydaje się to trochę zbyt homebrewed ... Jaki jest sposób Pythona, aby to osiągnąć?