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 function only calls the parent class from MRO order. Acc to this, your prefered class would be School and the init of School will be called. You have to give only school_name as parameter while calling super().__init__(self,school_name)
. But if you want to call particular __init__()
, then it is better to use <parent_class>.<method>(<parameters>)
. If you want to call both the init functions, try doing this:
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"
School.__init__(self,school_name)
Exam.__init__(self,exam_name)
test = Test('Success', 'Math')
print(test._school)
print(test._exam_name)
Try doing this