This question is pretty old, but I will add my answer for beginners anyway.
I ran into this problem myself, and here is a solution that worked for me:
class Grid: def __init__(self): self.matrix = [[0,1,2],[3,4,5],[6,7,8]] self.second_access = False def __getitem__(self, key): if not self.second_access: self.first_key = key self.second_access = True return self else: self.second_access = False return self.matrix[self.first_key][key] g = Grid() print(g[1][2]) # 5 print(g[0][1]) # 1 print(g[2][0]) # 6
Please note that this will not work for single access!
So, for example, if you want to use something from the form g[0] to get [0,1,2] , this will not work, and instead you will get a meaningless result (the object itself).
so.very.tired
source share