Search code examples
pythonmultiple-inheritance

Multiple inheritance in python "object.__init__() takes no parameter


i am working with inheritence,here i got output for single inheritence but multiple inheritence showing error.so please help me.I don't have any knowledge about mro in python.please give me good advice.

class Player:
    def __init__(self,name,country):
        self.name=name
        self.country=country

    def info(self):
        return self.name+":"+self.country

class Ipl(Player):
    def __init__(self,name,country,team):
        Player.__init__(self,name,country)
        self.team=team

    def info_ipl(self):
        return self.info()+"\nIpl team:"+self.team


x=Ipl("Suresh Raina","India","csk")    
print(x.info_ipl())


class Carrier:
    def ___init__(self,wicket,run):
        self.wicket=wicket
        self.run=run

    def disp(self):
        return "Wickets:"+self.wicket+"Runs:"+self.run



class Aauction(Ipl, Carrier):

    def  __init__(self,wicket,run,name,country,team):

        Ipl.__init__(self,name,country,team)
        Carrier.__init__(self,wicket,run)
        self.Innings=Innings


    def stati(self):
        return  self.info_ipl()+","+self.disp()+"Total Innings:"



x = Aauction(150,2000,"Suresh_Raina","India","kkr")
print(x.stati())

Above code giving following Error:-

Suresh Raina:India
Ipl team:csk
Traceback (most recent call last):
  File "C:\Users\Rahul\Desktop\PYTHON\EXP8.py", line 49, in <module>
    x = Aauction(150,2000,"Suresh_Raina","India","kkr")
  File "C:\Users\Rahul\Desktop\PYTHON\EXP8.py", line 40, in __init__
    Carrier.__init__(self,wicket,run)
TypeError: object.__init__() takes no parameters

Thank you.


Solution

  • I think the problem is that your __init__ has three underscores instead of two:

    class Carrier:
        def ___init__(self,wicket,run):
            self.wicket=wicket
            self.run=run
    
        def disp(self):
            return "Wickets:"+self.wicket+"Runs:"+self.run
    

    should be:

    class Carrier:
        def __init__(self,wicket,run):
             self.wicket=wicket
             self.run=run
    
        def disp(self):
             return "Wickets:"+self.wicket+"Runs:"+self.run