Called to implement subscription, that is, self[subscript]. See Subscriptions and slicings for details on the syntax.
There are two types of built-in objects that support subscription via __getitem__():
sequences, where subscript (also called index) should be an integer or a slice object. See the sequence documentation for the expected behavior, including handling slice objects and negative indices.
mappings, where subscript is also called the key. See mapping documentation for the expected behavior.
If subscript is of an inappropriate type, __getitem__() should raise TypeError. If subscript has an inappropriate value, __getitem__() should raise an LookupError or one of its subclasses (IndexError for sequences; KeyError for mappings).
Note: Slicing is handled by __getitem__(), __setitem__(), and __delitem__(). A call like
a[1:2] = b
is translated to
a[slice(1, 2, None)] = b
and so forth. Missing slice items are always filled in with None.
Note: The sequence iteration protocol (used, for example, in for loops), expects that an IndexError will be raised for illegal indexes to allow proper detection of the end of a sequence.
Note: When subscripting a class, the special class method __class_getitem__() may be called instead of __getitem__(). See class_getitem versus getitem for more details.