class School:
def __init__(self, school_name):
self._school = school_name
class Exam:
def __init__(self, exam_name):
self._exam_name = exam_name
def credit(self):
return 3
class Test(School, Exam):
def __init__(self, school_name, exam_name):
self._date = "Oct 7"
super().__init__(school_name, exam_name)
test = Test('Success', 'Math')
print(test._school)
print(test._exam_name)
I just want to know why super().init() can't work here. And if I insist to use super(), what's the correct way to do so that I can pass in the school_name and exam_name successfully.
super().__init__(school_name, exam_name)doesn't work because neither of the inherited implementations take two (three, withself) parameters. Also neither of them callssuperthemselves, so only one will get invoked anyway.__init__) you need to get the class, and an easy way to do that is justtype(self), then you can access the__mro__attribute which is a tuple of classes that your class inherits from. calling__init__onExamfromTest.__init__could then look like:type(self).__mro__[1].__init__(*args, **kwargs)