I want to extend the UserList class (https://docs.python.org/3/library/collections.html#collections.UserList) with new attributes like so:
class GroceryList(UserList):
def __init__(self, initlist, max_item_weight, max_item_volume):
super().__init__(initlist)
self.max_item_weight = max_item_weight
self.max_item_volume = max_item_volume
Now if I slice GroceryList, I lose max_item_weight and max_item_volume attributes so I override the __getitem__ method:
def __getitem__(self, i):
if isinstance(i, slice):
return self.__class__(self.data[i], self.max_item_weight, self.max_item_volume)
else:
return self.data[i]
How can I override the UserList __getitem__ method so that it is more generic and I don't have to override the __getitem__ method for every new type of list I create, e.g. GroceryList, ToolsList, NonEdibleList... because I want to keep the attributes even after slicing my derived lists.