The code is quite self-explanatory. I want to call both class A
and class B
__init__
methods.
Code:
class A(object):
def __init__(self, name):
self.name = name
class B(object):
def __init__(self, age):
self.age = age
class C(A, B):
def __init__(self, name, age):
A.__init__(self, name)
B.__init__(self, age)
def display(self):
print(self.name, self.age)
c = C("xyz", 12)
c.display()
Output:
xyz 12
I want to use super()
instead of explicitly stating
A.__init__(self, name)
B.__init__(self, age)
I am unable to find any resource for the same, need help.
The following does not work:
class A(object):
def __init__(self, name):
super(A, self).__init__()
self.name = name
class B(object):
def __init__(self, age):
super(B, self).__init__()
self.age = age
class C(A, B):
def __init__(self, name, age):
# A.__init__(self, name)
# B.__init__(self, age)
super(C, self).__init__(name, age)
def display(self):
print(self.name, self.age)
c = C("xyz", 12)
c.display()