for examplethe following class returns the square of an index value--atypical perhapsbut illustrative of the mechanism in generalclass indexerdef __getitem__(selfindex)return index * indexer( [ for in range( )print( [ ]end=' [icalls __getitem__(iruns __getitem__(xieach time intercepting slices interestinglyin addition to indexing__getitem__ is also called for slice expressions-always in xand conditionally in if you don' provide more specific slicing methods formally speakingbuilt-in types handle slicing the same way herefor exampleis slicing at work on built-in listusing upper and lower bounds and stride (see if you need refresher on slicing) [ [ : [ [ :[ [:- [ [:: [ slice with slice syntax ( - reallythoughslicing bounds are bundled up into slice object and passed to the list' implementation of indexing in factyou can always pass slice object manually--slice syntax is mostly syntactic sugar for indexing with slice objectl[slice( )[ [slice( none)[ [slice(none- )[ [slice(nonenone )[ slice with slice objects this matters in classes with __getitem__ method--in xthe method will be called both for basic indexing (with an indexand for slicing (with slice objectour previous indexing and slicing__getitem__ and __setitem__