I have the following code segment, which is shown as follows along with its output
from abc import ABCMeta, abstractmethod class AbstractClass(object, metaclass=ABCMeta): @abstractmethod def __init__(self, n): self.n = n print('the salary information') class Employee(AbstractClass): def __init__(self, salary, name): self.salary = salary self.name = name super(AbstractClass,self).__init__() emp1 = Employee(1000000, "Tom") print(emp1.salary) print(emp1.name)
I would like to let the subclass, e.g. Employee
, can also inherit the functionality implemented in the constructor of AbstractClass
. In specific, the print('the salary information')
I added:
super(AbstractClass,self).__init__() # which does not work
Advertisement
Answer
You need to pass the CURRENT class to super
, not the parent. So:
super(Employee,self).__init__()
Python will figure out which parent class to call.
When you do that, it won’t work, because AbstractClass.__init__
requires an additional parameter that you aren’t providing.
FOLLOWUP
It has been correctly pointed out that this will suffice:
super().__init__(0)