Python:Classes: last exercise

Python: Classes: last exercise

Hi Team

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

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?

Please help.

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, __init__ signature of list is the following:

  def __init__(self, *args):

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

class SortedList(list):
  def __init__(self, value):
    value.sort()
    print(value)
    
  
  def append(self, value):
    super().append(value)
    return self.append()
  

sort_list = SortedList([7,3,6])

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?

Thank you

but now if we tried to print sort_list:

class SortedList(list):
  def __init__(self, value):
    value.sort()
    print(value)
    
  
  def append(self, value):
    super().append(value)
    return self.append()
  

sort_list = SortedList([7,3,6])
print(sort_list)

we get an empty list, because the __init__ method of list class initializes the list. Which is now not happening.

*arg isn’t just some argument, which is best demonstrated with an example:

def example_func(*args):
   print(arg)

example_func(1, 2, 3, 4)

*args allows us to pass any number of positional arguments, quite powerful

i hope i covered this by adding print(sort_list), the list is just empty because the list is never initialized (which is done in the list class)

so now we also can’t use our append method you made. Doing the inheritance problem means the parent class can do a lot of heavy lifting for us