Make an object that behaves like a slice
How can we make a class represent itself as a slice when appropriate? This didn't work: class MyThing(object): def __init__(self, start, stop, otherstuff): self.start = start self.stop = stop self.otherstuff = otherstuff def __index__(self): return slice(self.start, self.stop) Expected output: >>> thing = MyThing(1, 3, 'potato') >>> 'hello world'[thing] 'el' Actual output: TypeError: __index__ returned non-(int,long) (type slice) Inheriting from slice doesn't work either. MisterMiyagi TLDR: It's impossible to make custom classes replace slice for builtins types such as list and tuple . The _