Python overloading multiple getitems / index requests

后端 未结 4 555
无人及你
无人及你 2021-02-06 01:54

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__()

4条回答
  •  清酒与你
    2021-02-06 02:34

    you could make the index into a tuple: def getitem(self,indexTuple): x, y = indexTuple ...

    and access the object override: instance[[2,3]]
    or instance[(2,3)]

提交回复
热议问题