Is there a way to implement __getitem__in a way that supports integer and slice indices without manually checking the type of the argument?
I see many examples of this form, but it seems very hoarse to me.
def __getitem__(self,key):
if isinstance(key,int):
if isinstance(key,slice):
On the other hand, why does this problem exist in the first place? Sometimes an int is returned, and sometimes a fragment is a strange design. The call foo[4]should call foo.__getitem__(slice(4,5,1))or similar.
rophl source
share