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


#1

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.


#2

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__().