So I have this code :
JavaScript
x
11
11
1
class matrix:
2
def __init__(self, matriceOrigine: list) -> None:
3
self.length = len(matriceOrigine)
4
self.matrice = list(matriceOrigine)
5
6
def transpose(self) -> list:
7
pass
8
9
def transition(self, lam: float) -> list:
10
pass
11
And when I create an instance, like this :
JavaScript
1
3
1
foo = [[1,2,3],[4,5,6]]
2
foo2 = matrix(foo)
3
To access a value (like 3 for example), I know I should do
JavaScript
1
2
1
foo2.matrice[0][2]
2
and I would like to know how to access it using
JavaScript
1
2
1
foo2[0][2]
2
and still use
JavaScript
1
3
1
foo2.transpose()
2
foo2.length
3
Thanks in advance !
Advertisement
Answer
Define the __getitem__
function to allow for custom indexing:
JavaScript
1
3
1
def __getitem__(self, index):
2
return self.matrice.__getitem__(index)
3