Hello awesome community, I was learning the OOPS concepts with python as a part of my curriculum. I am having a problem with multiple inheritance in python. The following is my code:
#!/usr/bin/env python
class Base1(object):
def __init__(self):
self.base1var = "this is base1"
class Base2(object):
def __init__(self):
self.base2var = "this is base2"
class MainClass(Base1, Base2):
def __init__(self):
super(MainClass, self).__init__()
if __name__ == "__main__":
a = MainClass()
print a.base1var
print a.base2var
and when running, I am getting the following error
print a.base2var
AttributeError: 'MainClass' object has no attribute 'base2var'
If I swap the order of classes inherited, the variable name in the error changes accordingly.
Am I using super()
wrong when I want to call the constructors of the two inherited classes?
How can I correctly inherit from multiple base classes and use the variables and methods in the main class without this error?
Thank you.