For the last exercise in Classes module, it was asked
Some things to consider:
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?
What other Python builtins have functionality “missing”? Could you write a new dictionary that uses a fallback value when it tries to retrieve an item and can’t?
For the first instruction, I tried using constructor init. My code is such
I inherited from class list and trying to return the sorted list as soon as object gets created.
Please ignore the append method as that corresponds to another instruction in the exercise.
I don’t get any error but a blank list is returned.
the __init__() has a void signature, it shouldn’t return anything at all. Also, adding a __init__ will overwrite the init method of the parent (list), so be careful with that. That is why super() was added to append method
also, .sort() sorts the list in place and doesn’t return anything (so you get None, the absence of a retur n value)
so we can use that in our SortedList class, make sure to call super() as well. Now i will leave you to puzzle the last pieces together, let me know if you need more help
I did this and got the output.
It really makes me wonder why do super at all for init if I won’t be using it’s initial definition in parent class at all.
Also, I think that way coz I do not understand what init actually does. We take some argument in it and it assigns those value to self.agr*. What role does that play?
Please help me understand its role. I mean alright it instantiates the objects( I guess) … but what does instantiating them in init help in long run?