The indices method of the slice object will, given the length of the sequence, provide a canonical interpretation of the slice, which you can pass to xrange :
def __delitem__(self, item): if isinstance(item, slice): for i in xrange(*item.indices(len(self.l))): print i else: print operator.index(item)
Using slice.indices ensures correct behavior in cases marked by dunes . Also note that you can pass the slice object to list.__delitem__ , so if you need to do some preprocessing and delegate the actual deletion to the base list, the βnaiveβ del self.l[i] will work correctly.
operator.index will ensure that you get an early exception if your __delitem__ receives an unclassified object that cannot be converted to an index.
source share