Search code examples
pythonmultiple-inheritance

TypeError: __init__() takes 4 positional arguments but 7 were given


class Employee(object):
    def __init__(self,ename,salary,dateOfJoining):
        self.ename=ename
        self.salary=salary
        self.dateOfJoining=dateOfJoining
    def output(self):
        print("Ename: ",self.ename,"\nSalary: ",self.salary,"\nDate Of 
Joining: ",self.dateOfJoining)
class Qualification(object):
    def __init__(self,university,degree,passingYear):
        self.university=university
        self.degree=degree
        self.passingYear=passingYear
    def qoutput(self):
        print("Passed out fom University:",self.university,"\nDegree:",self.degree,"\Passout year: ",self.passingYear)
class Scientist(Employee,Qualification):
    def __int__(self,ename,salary,dateOfJoining,university,degree,passingYear):
        Employee.__init__(self,ename,salary,dateOfJoining)
        Qualification.__init__(self,university,degree,passingYear)
    def soutput(self):
        Employee.output()
        Qualification.output()
a=Scientist('Ayush',20000,'21-04-2010','MIT','B.Tech','31-3-2008')
a.soutput()

I'm not able to get a solution to the problem and I'm not able to understand why this TpyeError has occured. I'm new to python. Thanks


Solution

  • Your scientist class has the init function written as :

    def __int__
    

    instead of

    def __init__
    

    So what happened is it inherited the init function from its parent class, which receives less params then you sent to the class.

    Also instead of calling the init of the parents you should be using the super function.

    super(PARENT_CLASS_NAME, self).__init__()
    

    This obviously goes for all parent functions.