When do I need to use super()?

Which INHERITS from list. No comment on the rest.

2 Likes

Hello,
can you tell if my understand ing is true ?

when we use self in inherited class and use it inside method that is already present in parent class super() [ I think it should be a function returns class because we use DOT notation on it ??? ] instruct a interpreter : Hey I’m going to modify a method we have already in parent class but I need you to copy a parameters i give to super() to you copy them for me and save me from typing all them again ?

also we could have same functionality by defining new iter method instead of using super() keyword copy and paste all variables and things inside parent class iter method and have same functionality if use super() It just copy what we tell for us for our new re defined method in this case iter

hey @mtf,
How does this self refers to the the list, just because the parent class is list and there is no other parameters?

If we instantiate a SortedList we are given an empty list. The instance object is self, the context or owner object.

class SortedList(list):
  def __init__(self, value):
    self = value
    self.sort()
    print(self)
  def append(self, value):
    super().append(value)
    self.sort()
sorted_list = SortedList([4, 1, 5])
print(sorted_list)

output:[1, 4, 5]

what’s wrong with this code why it is giving output a blank list .
but when i am using super().init(value) instead of self= value then it is giving the right answer.
can anyone explain it to me what’s the difference between both and what i am not getting right?