For the last exercise in Classes module, it was asked
Some things to consider:
- When a
SortedListgets initialized with unsorted values (say if you call
SortedList([4, 1, 5])) those values don’t get sorted! How would you change
SortedListso 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
class SortedList(list): def __init__(self, value): return value.sort() def append(self, value): super().append(value) return self.append() sort_list = SortedList([7,3,6]) print(sort_list)
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.
What am i doing wrong?