Next / Previous / Contents / TCC Help System / NM Tech homepage

26.3.16. __getitem__(): Get one item from a sequence or mapping

If a class defines it, this special method is called whenever a value is retrieved from a sequence or mapping (dictionary-like object) using the syntax “v[i]”, where v is the sequence or mapping and i is a position in a sequence, or a key in a mapping.

Here is the calling sequence:

    def __getitem__(self, i):
        ...

The method either returns the corresponding item or raises an appropriate exception: IndexError for sequences or KeyError for mappings.