The instance[indice]
syntax triggers a call for instance.__getitem__
with indice
as the argument.
This shortcut allows also the use of the syntax x:y:z
to represent slice(x, y, z)
, which is usually how it is used, but it could be fitted also for other types of indexes, like tuples or strings, as long as your __getitem__
supports these.
In this code it is used as part of numpy's way to slice 2-dimensional arrays, with the tuple containing the slices for each dimensions.
For future reference, you can test with this class:
>>> class sliced:
... def __getitem__ (self, index):
... print(index)
>>> d = sliced()
>>> d[:50, 1]
(slice(None, 50, None), 1)
for that particular case, the comma makes the index a tuple (like 1, 2
would if typed in the REPL), whose first item is the :50
which is evaluated as a slice with no start, end at 50
and no step specified (the x:y:z
notation fills None
in the blanks, and does not require the second :
).