I have a simple class that takes a List of objects as an input, and I want to ensure I can perform slicing on that operation.
class MyClass(AbstractLocationClass):
def __init__(locations=None, **kwargs):
if locations is None:
locations = []
self._locations = locations
#... do other stuff with kwargs..
I want to allow users to do the following:
m = MyClass(locations=[[1,2],[2,3],[3,4]])
sliced = m[0:1]
print sliced
>>> [[1,2],[2,3]]
I know I have to override __getitem__
but what I'm unsure of is how to handle all the notation types like obj[0], obj[1:2], etc...
Can someone please advise on the proper way of implementing this feature.