Call to __init__ of super class is missed
Call to __init__
of super class is missed
Origin
If a parent class declare __init__
method explicitly, even if that method is empty
then the sub class's __init__
method need to invoke parent's __init__
if not, a warning Call to __init__ of super class is missed
will be thrown by PyCharm.
Tracing
Why? Please follow me, look at the first demo
Error Demo
1 | class Animal: |
Fine 1
If a sub class do not override __init__
method, parent's attributes will be inherited.
1 | class Animal: |
Fine 2
If a sub class want to declare its own attributes and inherit its parent's attributes, do as follows:
1 | class Animal: |
Fine 3: Multiple Inheritance - solution 1
If a sub class inherits from multiple parent class, you should do like as follows.
If you do not explicitly invoke parent's __init__
for each parent class in Subclass's __init__
, it will only inherit the first
parent class's attributes.(The first parent class in the following code is Engine class.)
1 | class Engine: |
Fine 3: Multiple Inheritance - solution 2
Or like this:
(If you are not sure the parent class's behavior, Solution 1 is very good.)
1 | class Engine: |
Conclusion
At the most time(Or always. Unless you know why you need not invoke init), you should invoke parent's init in every subclass init method.