Search code examples
python-3.xmultiple-inheritance

Multiple Inheritance: takes 0 positional arguments but 1 was given


In testing multiple inheritance, I have the follow Date, Time and DateTime class heirarchy

class time:
    def __init__(self, time):
        self.time = time 

    def getTime():
        return self.time;

class date:
    def __init__(self, date):
        self.date = date

    def getDate():
        return self.date

class datetime(time,date):
    def __init__(self, input_time, input_date):
        time.__init__(self, input_time)
        date.__init__(self, input_date)

Instantiating and checking the date works fine:

my_datetime = datetime("12PM","Today")
my_datetime.date
'Today'

But running the getDate function yeilds a parameter error and I don't understand why

my_datetime.getDate()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-120ecf58a608> in <module>
----> 1 my_datetime.getDate()

TypeError: getDate() takes 0 positional arguments but 1 was given

Solution

  • Your issue has nothing to do with the multiple inheritance issue. In fact, you'd get exactly the same error trying to call getDate on an instance of date.

    The cause of the issue is that you've forgotten to list self as an argument to getDate (and time.getTime as well). The instance the method gets called on will be passed automatically as the first positional argument, so you need to write the method with that in mind.