Bonus Challenge Discussion

How do we get repr to represent the answer correctly? My code is:

class SortedList(list):
  def __init__(self, lst):
    super().__init__(lst)
    self.lst = lst
    self.lst.sort()
  
  def __repr__(self):
    return str(self.lst)

  def append(self, value):
    super().append(value)
    self.sort()


print(SortedList([99, 6, 7]))

Also, why do we need to use super().__init__(lst) in our initialization? When I tried removing that line I got an error message.

I fixed the above code so it works correctly now, and I’ve realized that super can take another clarifying argument (see When do I need to use super()?). I’ve also tried the dictionary example, and I found another bit of weird behavior.

class BetterDict(dict):
  def __init__(self, dictionary):
    self.dictionary = dictionary
    super().__init__(dictionary)

  def get(self, key):
    if key in self.keys():
      return self.dictionary[key]
    else:
      return "No key found!"

my_dict = BetterDict({5 : "five", 3 : "three"})

print(my_dict.get(6))
# returns "No key found!"
print(my_dict.get(5))
# returns 5

When I remove the second line in init, my second print test returns the error message. Why is this? What does super().__init__(dictionary) do, exactly?