Your original question had a list that passed as an index, so I wrote this assuming you want to access the (i, j) th element from a 2D list. You can do this by defining your class with something like:
class MyJavaCollection: def __init__(self, values): self.values = values def __getitem__(self, indices): """ Returns the item at index (i, j) given an index list [i, j]. """ return self.values[indices[0]][indices[1]] def __setitem__(self, indices, value): """ Sets the (i, j)th item to the input value given an input index list [i, j]. """ self.values[indices[0]][indices[1]] = value
Here you overload the __getitem__ and __setitem__ methods to retrieve or set the (i, j) th element in the values list when passing the list of indices [i, j] . If your values ββare just numbers, then the syntax myCollection([1, 1]) += 10 will add 10 to values[1][1] .
As indicated in another answer, if you are not just storing the numbers in your object, you may need to overwrite the __add__ or __iadd__ in any class that contains your data in order to get the desired behavior.
Testing my class class:
>> my_list = [[1, 2, 3], [4, 5, 6]] >> my_list[1][1] 5 >> my_collect = MyJavaCollection(my_list) >> my_collect[[1, 1]] 5 >> my_collect[[1, 1]] += 5 >> my_collect[[1, 1]] 10
The documentation on special method names gives you everything you might know about special methods like this. This can be a good place to look if you donβt know which method you might need to overload.