In this exercise, the SortedList class has no constructor. Why does it not need one?
Answer
The SortedList class inherits from the Python list class. The list class provides the implementation to store data. The SortedList class doesn’t need to change the storage of data only the way that the data is added, so SortedList objects will use the constructor from the parent list class and only provide an implementation for the append() method.
Even though it does not need a constructor, I still added one, (shown below) because if I did not, then when I tried to instantiate an object like some_list = SortedList([1,6,3,7]), I got an error about SortedList not taking any other arguments other than self. Hence the first two lines below. I then added a line self.sort() But then when I printed (some_list.list) I got the list unsorted so I had to use the thrid line which solved that problem. I added the fourth line just to check.
def init(self, a_list):
self.list = a_list
self.list.sort()
print(self.list)
Answer to following question:
When a SortedList gets initialized with unsorted values (say if you call SortedList([4, 1, 5]) ) those values don’t get sorted! How would you change SortedList so that the list is sorted right after the object gets created?
Hello and greetings from Alaska. This is my first comment, I hope I am not being a bother.
So, the reason we don’t use an def init(self): is because the primitive -list- already has one?
Thank you for your time.
Yes it’s a method that already exists in a parent class. If we don’t over override it in our new class (by defining a new .__init__ function) then the .__init__ method of the parent class is used (the list type here) which is what gives us the basic set up of the object (the values are added to the indexed and ordered mutable sequence).
A few of the examples above shows what happens if we do want to add a new behaviour to the initialisation. We want to write our own additional steps for initialisation using a newly defined __init__ method but we also still want to initialise the elements of our sequence using an iterable like the parent list class does. So what we do in that case is first call the parent classes .__init__ (using super here) and then perform any additional steps we’d like for initialisation (in this case, sorting).