Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Is it mandatory to call superclass init inside the class init?
Hello all,

Given a class A and a class B inherited from A, is it mandatory to call A.__init__(self) from B __init__ or could be possible to call it in other function?

Thank you in advance!
No it is not mandatory, it is not even mandatory to call it from another function. However if you start doing strange manipulations at instance's initialization, it probably means that this class hierarchy is not the best one for your problem.
No it is not, but it is good practice to do so and it is the expected way things work...

The __init__ function is used to set everything up when you create an instance of the class, so users of class B will assume that this has been done and if not, then it may produce bugs or side effects the user does not understand.

Furthermore if the developer of class A changes the implementation and now requires __init__ to be called in order for it to work correctly it will cause unknown issues for the use of class B.

Lots of time can be lost in tracking down and fixing bugs in this kind of situation, so best stick to the usual practice unless there is a very very good reason for doing so.
There is no passion to be found playing small - in settling for a life that is less than the one you are capable of living.
It's not mandatory. But if you want the A.__init__ method to handle its own initialization and B.__init__() just something bit more, you wave to call it.

And the proper way is to call super():

class B(A):
    def __init__(self):
        super.__init__() # instead of A.__init(self)
When you use multiple inheritances don't calling super() could cause some issues.

In [1]: class A:
   ...:     def __init__(self):
   ...:         print('A')

In [2]: class B(A):
   ...:     def __init__(self):
   ...:         A.__init__(self)
   ...:         print('B')

In [3]: class C(A):
   ...:     def __init__(self):
   ...:         A.__init__(self)
   ...:         print('C')

In [4]: class D(B, C):
   ...:     def __init__(self):
   ...:         B.__init__(self)
   ...:         C.__init__(self)
   ...:         print('D')

In [5]: obj = D()
As you can see A is called twise.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to Call a method of class having no argument dataplumber 7 514 Oct-31-2019, 01:52 PM
Last Post: dataplumber
  Call method from another method within a class anteboy65 3 257 Sep-11-2019, 08:40 PM
Last Post: Larz60+
  Call a varaible from class in the parent class Clement_2000 1 539 May-09-2019, 11:14 PM
Last Post: michalmonday
  How to call base class function kamal_chennai 1 380 Apr-03-2019, 01:12 PM
Last Post: ichabod801
  Get variable from class inside another class hcne 3 515 Mar-30-2019, 03:02 PM
Last Post: ichabod801
  Why Can't I call base class function for my derived class object? AmirAB 2 463 Feb-13-2019, 03:04 PM
Last Post: snippsat
  Calling a class inside other class dan789 9 694 Jan-06-2019, 02:00 PM
Last Post: dan789
  a Word Inside Parenthesis in Class Decleration tahasozgen 2 622 Dec-18-2018, 05:59 AM
Last Post: Gribouillis
  makingVariable declaration mandatory rohitnirantar 5 657 Aug-26-2018, 08:00 AM
Last Post: Gribouillis
  Mask superclass __init__ fig0 5 1,031 Jan-11-2018, 06:59 PM
Last Post: Gribouillis

Forum Jump:

Users browsing this thread: 1 Guest(s)