When do I need to use super()?

Hi, I have a question about the end of the polymorphism section (Review). I don’t understand why we have to call super() to apply append() method on our object SortedList but we don’t have to do it when we apply sort() method in the next line:

class SortedList(list):

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

Is it because we apply super().append(value) before and SortedList already behaves like a list?

Thanks for helping me!

In this case it doesn’t change much because we have not overwritten sort() in any way, so calling self.sort() will simply use the default sort() which we inherited from the list class, so using super() or not wouldn’t matter.

However, if we had modified sort(), then there could be a significant difference between calling self.sort(), that is, calling the sort() from our LinkedList class, and calling the original sort() from the list class.

1 Like

Thank you so much for your explanation, I didn’t think about the fact we were overwriting the append() method but not sort(), I understand now :slight_smile:

For the record, overridden is not synonymous with overwritten. We are not overwriting a parent method, but overriding it.

2 Likes

What is the difference between the two in Python?

I dont’ know that Python has a term, overwrite which means simply to replace some text with some other text. Override on the other hand means ‘to prevail over’ or ‘supersede’. The parent class method is ‘set aside’ in favor of the derived class method.

4 Likes

But aren’t we replacing text with some other text given that we’re modifying the behavior of a method when we’re overriding it? I mean, we’re literally rewriting the method, so I don’t know, seems pretty similar in this context.
But thanks for the correction, if that’s simply the terminology used I guess it’s more useful to use it, I hope it’ll stick into my memory.

1 Like

No, the overriding method is not replacement text, but the same of similar text by the same variable name in another class. It’s the existence of that other method in the subclass that permits it to override the parent.

For our own sake, we should firm up what terminology to use in any situation. It’s hard enough to learn the language, as it is. We don’t want to muddle ourselves into a bent way of thinking.

2 Likes

Quick question about the append() method. With the line: super().append(value) how does the code know which list to append the value to? For example when using append() normally, the syntax would be: listName.append(value). But here, super() is not the name of our list we want to append to. Basically my question is how come we don’t have to write something like: self.list.super().append(value) ?
I hope this question makes sense, thanks for your help.

Your interpretation is pretty close, but remember that self is itself a list. super() takes self along with it, saying something like, “grab this method from the parent class and run it on self.”

3 Likes

Ah i see, so the self is implied when you use super()? But isn’t self in this case our new class SortedList, not exactly a python list. So how does it know how to run append() on a new class we created? Is it because the only parameter of the class is a python list? would it not have worked if we had multiple different parameters with different types? Thanks for the response!

calling super without arguments is a bit magical (doesn’t follow normal behaviour, where does it get self from, where does it get the current class from? Magic.)
It’s obvious what’s meant, but actually typing it is a real head-scratcher.

What you get back when calling super is some kind of proxy which refers to the object in question (self), but attribute lookup skips the current class when resolving the attribute, as if it did not exist in the current class

I imagine it skips any child classes too. Search upwards, being the idea.

1 Like

self is an object that is a single instance of the class SortedList(); its type is <class SortedList> .

Ah! I think so; it certainly seems that the SortedList object appears to the parent to be a conventional list, for which append() is a valid method.


According to the docs, super() can take as optional parameters (1) the type of the object being passed, and (2) the object itself. Thus,

super().append(value) is equivalent to super(SortedList, self).append(value)
(You can try it out in the exercise - it works!) This, I think, grounds the “magic” to which @ionatan referred.

Also from the docs: super()

… is useful for accessing inherited methods that have been overridden in a class.

Which is exactly what we are doing here with respect to append(). (You can use either self.sort() or super().sort(), since our method is not overriding the conventional obj.sort() method.)

1 Like

Yeah the magical part is that the arguments can be left out. Python gives it special language-level treatment. Or maybe it’s just through inspection of things “under the hood” that are still visible in python. Either is magical behaviour though.

2 Likes

From what I understand it appears that “overwriting” would change the behavior/original definition of the method in the parent as well as the child, while “overriding” means the behavior of the method in the parent remains unchanged even though the child is using a modified version of the method and “overriding” the original method from the parent.

then if it is because we did not modify sort() and then it is using the default behaviour of sort() from the List class, then what is the need of calling supper() on append(). it should have used the append() just like it used sort(). And if one should say that the SortedList class tried modifying the behaviour of append(), I do not see how it was modified. it just called it to do its thing on the value.
please help me throw more light on this cos I am really confused. Thanks a lot.