Hi, I just finished the section on polymorphism, and here is what I came up with in order to make the SortedList to be sorted at initialization (spoilers, if you want to solve this bonus yourself, don’t look):
class SortedList(list): def __init__(self, values): super().__init__(self) self.values = values self.values.sort() def append(self, value): super().append(value) self.sort() def __repr__(self): return str(self.values) a = SortedList([3, 45, 1234, 21]) print(a)
And it works, but it seems that it forces me to keep the values outside of the parent class, that is, it seems I can’t make the
super().__init__() function initialize the values I receive for me, I tried passing them directly to it, but it said that it only accepts one argument: self.
And this of course then forces me to overwrite
__repr__(), because otherwise when I try to print my SortedList it prints an empty list (since the values are not accessible from the parent class).
So I would just like to know if there’s something I’m doing wrong/not optimized or if it’s how it’s supposed to work? In a way it would also make sense to work like that, because otherwise I don’t know how you would access the values in the subclass (maybe from a return from super() ?)
Thanks to anyone who takes the time to answer me.