Home > other >  Python - Inheritence - Subclasses - TypeError: __init__() takes 4 positional arguments but 5 were gi
Python - Inheritence - Subclasses - TypeError: __init__() takes 4 positional arguments but 5 were gi

Time:09-11

I have gone through several solution posted here for similar problem/Error, but it didn't help me to fix this issue.

class Employee:
    raise_amount = 1.05
    def __init__(self, first, last, pay):
        self.first = first
        self.last = last
        self.pay = pay


class Developer(Employee):
    raise_amount = 1.30
    def __int__(self, first, last, pay, prog_lang):
        self.prog_lang = prog_lang
        super(Employee).__init__(first, last, pay)



print(help(Developer))
emp_1 = Developer('Iama','learner',2000 , 'Python')

I am getting error at last line as shown below. Could you help me figure out what is missing in my code?

 emp_1 = Developer('Iama','learner',2000 , 'Python')
TypeError: __init__() takes 4 positional arguments but 5 were given 

CodePudding user response:

Here in the Developer class there has int rather than init.

that is why the error raise as You have inherited the Employee class into Developer therefor the Employee class init method is inherited into Developer

that is why the Employee init method has only (self, first, last, pay): argument to accept, and you are passing the the Developer('Iama','learner',2000 , 'Python') is passing the 5 including self and the developer class has the init of Employee class therefor the error raise.


class Employee:
    raise_amount = 1.05
    def __init__(self, first, last, pay):
        self.first = first
        self.last = last
        self.pay = pay


class Developer(Employee):
    raise_amount = 1.30
    def __init__(self, first, last, pay, prog_lang): # rename __int__ to __init__ it is auto suggestion issue you have made 
        self.prog_lang = prog_lang
        super(Employee).__init__(first, last, pay)



print(help(Developer))
emp_1 = Developer('Iama','learner',2000 , 'Python')
  • Related