Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

What's the difference between:

class Child(SomeBaseClass):

def __init__(self):

super(Child, self).__init__()

and:

class Child(SomeBaseClass):

def __init__(self):

SomeBaseClass.__init__(self)

I've seen super being used quite a lot in classes with only single inheritance. I can see why you'd use it in multiple inheritances but am unclear as to what the advantages are of using it in this kind of situation.

1 Answer

0 votes
by (106k points)

We use super() method in Python because it has some benefits which are as follows:- 

Mostly super() in single-inheritance are minimal, so you don't have to hard-code the name of the base class into every method that uses its parent methods.

But it is almost impossible to use multiple-inheritance without super(). Because the use of super() in multi-inheritance includes common idioms like mixins, interfaces, abstract classes, etc. If somebody later wanted to write a class that extended Child and a mixin, their code would not work properly.

Related questions

+1 vote
2 answers
0 votes
1 answer
asked Jun 27, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
+1 vote
3 answers
asked May 18, 2019 in Python by Ayush (46k points)
0 votes
1 answer
asked Feb 16, 2021 in Python by laddulakshana (16.4k points)

Browse Categories

...