Does the constructor for an inherited class have to call the parent constructor?

Question

Is it required that the constructor for an inherited class call the parent class __init__() method?

Answer

No, an inherited class is not required to call the __init__() method of the parent class. If no __init__() method is implemented in the inherited class, then the parent __init__() will be called automatically when an object of the inherited class is created. If __init__() is implemented in the inherited class, then that will override the parent class method. The parent class method will NOT be called unless the call is written in the inherited class.

8 Likes

If the derived class has any input parameters in common with the parent class, then it should invoke the super() function on those parameters so they get sent to the parent class for instantiation. In other words, if the parent has a self.name attrtibute, then the derived class should send its name parameter to the parent.

super(name)

This brings us to the question as to whether __init__() is a constructor or not I reason that it is not since we can invoke an instance without its presence.

>>> class Foo(object):
    pass

>>> foo = Foo()
>>> foo.__class__
<class '__main__.Foo'>
>>> foo.__class__()
<__main__.Foo object at 0x02E110F0>
>>> 

Obviously the object is already instantiated. Anything done upon that object is not constructing it, but instructing it, as in initialization. The method name says it all… __init__().

18 Likes

I have a question. If not init method is supplied, I understand that the init of the parent class is called automatically. Is there something similar to the Object class in python from which every class is derived (if not explicitly inheriting from some class) ?
If this is the case, some constructor will always be called, either explicitly or implicitly…

2 Likes

Yes, I believe so. In Python 2 we used to write,

class Foo(Object):

but in Python 3 that became moot, as in de facto. All classes inherit from the Object class. Do check into this further in your reading, but don’t get too hung up on it. Let the lessons progress and do your diligence by taking up a little reading every day that pertains to and sheds more light upon and insight into the day’s lessons.

4 Likes

@mtf Hello,

To override a method is to use the same name for a method in the subclass, but if the subclass uses a method wich builds up on its fathers method it’s better to invoke super()?
So we could just add what is necessary without repeating code.

That’s the idea I have of using super(), if I’m wrong can you explain your point of view on when to use wich?

What if we have two parent class .And we have to inherit only one class and not the other.
How do we do that?

1 Like

Do you mean something like,

class A:
    pass
class B(A):
    pass
class C(B):
    pass

?

I mean somehting like this.

class A:
pass
class B:
pass
class C(A, B):
pass

How do we do

super()

in class C for a specific class.

This might shed some light…

https://www.python-course.eu/python3_multiple_inheritance.php

1 Like

super() is only used for attributes we do not wish to override.

4 Likes

class Triangle:
def init(self, base, height):
self.base = base
self.height = height

def area(self):
    return 0.5 * self.base * self.height

class RightPyramid(Triangle, Square):
def init(self, base, slant_height):
self.base = base
self.slant_height = slant_height

def area(self):
    base_area = super().area()
    perimeter = super().perimeter()
    return 0.5 * perimeter * self.slant_height + base_area

@mtf

  • super()*

can automatically find the attributes of class without mentioning it but it does in the order that we have inherited the parent class.
If both class has the same attributes and we use super() it assign the value of the parent class which we have inherited first.
This what I understand from the above .
Am I correct from my understanding?

1 Like

It looks like you’re on the right track. The parent class that has the attribute is the one the child class will access.

2 Likes

Alright, thank you!:fire:

2 Likes

Is that Python 2 or Python 3 code?