Implementing slicing PREMIUM

Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
5 min. read 4 min. video Python 3.8—3.12

How can you make your Python object support slicing?

Indexing relies on __getitem__, but so does slicing!

Python's subscript notation ([...]) relies on the __getitem__ method.

Here's a class with a __getitem__ method:


A Python Tip Every Week

Need to fill-in gaps in your Python skills? I send weekly emails designed to do just that.