I am trying to develop a class structure. I ran into problems trying to access attributes of parent classes (i.e. when I inherited BaseX and BaseY, the attributes of BaseX appeared in child class but not of BaseY) trying to inherit parent classes and went through different posts here and finally came to below structure which is similar to what I am doing with my actual code; I have been more experimental with the structure below though just to understand concepts. The actual code starts with a single parent class, then multiple layer 1 child classes inherit it, then there are further layer 2 child classes which are less than the number of layer 1 child classes and then I merge all layer 2 child classes in to one class. Now after a lot of R&D, I found a code below which is doing what I want (can access all attributes in class SubABC) but I am not sure if this is correct as despite achieving this, I am not clear on how exactly this super() is working here. And I would like to be clear with what I am doing before proceeding. For example, why single super() is sufficient in MidA and MidB but requires three super() in MidC. And why SubABC requires only one super(). I would appreciate some guidance here before I replicate this in to my code. I am no Python expert so please do point me in right direction if I need to read something - i.e. documentation on super(). Also, PyCharm (2019.3.1 - Community Edition) complains about MidA super().init(x_var, z_var) and MidB super().init(x_var, z_var) statements saying 'unexpected arguments'; not sure if it is just a bug. Thanks.
class BaseX(object):
def __init__(self, x_var):
print('++++ Class BaseX')
self.x_var = x_var
print('---- Class BaseX')
class BaseY(object):
def __init__(self):
print('++++ Class BaseY')
self.y_var = 'Y_Var'
print('---- Class BaseY')
class BaseZ(object):
def __init__(self, z_var):
print('++++ Class BaseZ')
self.z_var = z_var
print('---- Class BaseZ')
class MidA(BaseX, BaseY, BaseZ):
def __init__(self, x_var, z_var):
print('++++ Class MidA')
super().__init__(x_var, z_var)
print('---- Class MidA')
def mid_a_func(self):
print(self.x_var)
print(self.y_var)
print(self.z_var)
class MidB(BaseX, BaseY, BaseZ):
def __init__(self, x_var, z_var):
print('++++ Class MidB')
super().__init__(x_var, z_var)
print('---- Class MidB')
def mid_b_func(self):
print(self.x_var)
print(self.y_var)
print(self.z_var)
class MidC(BaseX, BaseY, BaseZ):
def __init__(self, x_var, z_var):
print('++++ Class MidC')
super().__init__(x_var)
super(BaseX, self).__init__()
super(BaseY, self).__init__(z_var)
print('---- Class MidC')
def mid_b_func(self):
print(self.x_var)
print(self.y_var)
print(self.z_var)
class SubABC(MidA, MidB, MidC):
def __init__(self, x_var, z_var):
print('++++ Class SubABC')
super().__init__(x_var, z_var)
print('---- Class SubABC')
def sub_ab_func(self):
self.mid_a_func()
ab = SubABC('X_Var', 'Z_Var')
ab.sub_ab_func()
The output is:
++++ Class SubABC
++++ Class MidA
++++ Class MidB
++++ Class MidC
++++ Class BaseX
---- Class BaseX
++++ Class BaseY
---- Class BaseY
++++ Class BaseZ
---- Class BaseZ
---- Class MidC
---- Class MidB
---- Class MidA
---- Class SubABC
X_Var
Y_Var
Z_Var