0

I have a Python class with __getitem__ that can receive a single integer and return some value,

class Coeffs:
    def __getitem__(self, k):
        # some computation
        return 2 * k ** 3 / 5


c = Coeffs()
print(c[4])
25.6

I would now like to have this class support slices à la c[1:4] by just calling the integer-version __getitem__ on all values in the slice.

Is there any simple or canonical way to do this?

Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249

0 Answers0