Home > database >  TypeError: __init__() missing 4 required positional arguments ; Even when arguments are provided
TypeError: __init__() missing 4 required positional arguments ; Even when arguments are provided

Time:02-25

I want to make a child class (Science) that inherits from two parent classes (Person and Student; where Student inherits from Person). The two parent classes have their own methods that print out information when an object is passed to them, both the parent classes have one method by the name 'obj_type', in the 'Student' subclass the 'obj_type' of Person is overidden . My task is to create a child class that contains a method that prints each method from both the classes. I use the following code:

class Person:
    def __init__(self,name,day,month,year):
     self.name= name
     self.day= day
     self.month= month
     self.year= year


    def obj_type(self):
        print (f"{self.name} is a person!\n")

    def intro(self):
        print(f"The name of the person is {self.name}.\nTheir day of birth is {self.day}.\nTheir month of birth is {self.month}.\nTheir year of birth is {self.year}.")


class Student(Person):

    def __init__(self,name, day, month, year,sid,uni,gryear):
        super().__init__(name, day, month, year) #Using the super() method to redirect some attributes to the parent class
        self.studentID = sid
        self.university = uni
        self.graduationyear = gryear
       def obj_type(self):
            print (f"{self.name} is a person and student!\n")
   
    def stdintro(self):
           print(f"The ID of the student is {self.studentID}.\nTheir university is {self.university}.\nTheir graduation year is {self.graduationyear}.")


class Science(Student,Person):
    def __init__(self,name, day, month, year, sid, uni, gryear):
        Student.__init__(self,sid, uni, gryear)
        Person.__init__(self,name, day, month, year)

    def finalcall(self):
        Person.intro(self)
        Person.obj_type(self)
        Student.stdintro(self)
        Student.obj_type(self)

Yash= Science("Yash",3,'April',2000,123456,'University',2029)
Yash.finalcall()

Afer this, even though I have passed all the required arguments I get the error:

Traceback (most recent call last): TypeError: init() missing 4 required positional arguments: 'year', 'sid', 'uni', and 'gryear'

Can someone tell me what I am doing wrong here?

CodePudding user response:

The problem is in your Science class, you try to create a Student but with only 3 arguments. Change your Student.__init__(self,sid, uni, gryear) with Student.__init__(self,name, day, month, year, sid, uni, gryear) in your Science init and it should be fixed. As Karl Knechtel said in comments

CodePudding user response:

You are calling Student.__init__ with 4 arguments inside Science.__init__, but Student.__init__ expects quite a few more.

You should be using super consistently in all the classes, or not at all. See Python's super() considered super! for an expanded discussion on why keyword arguments are used to instantiate the class.

class Person:
    def __init__(self, *, name, day, month, year, **kwargs):
        super().__init__(**kwargs)
        self.name = name
        self.day = day
        self.month = month
        self.year = year

    ...



class Student(Person):

    def __init__(self, *, sid, uni, gryear, **kwargs):
        super().__init__(**kwargs)
        self.studentID = sid
        self.university = uni
        self.graduationyear = gryear

    ...


class Science(Student):
    # No need to override __init__ if all it does is
    # call super().__init__

    ...


Yash = Science(name="Yash", day=3, month='April', year=2000, sid=123456, uni='University', gryear=2029)
Yash.finalcall()
  • Related