I have classes structure like this:
class A(object):
array = [1]
def __init__(self):
pass
class B(A):
array = [2, 3]
def __init__(self):
super(B, self).__init__()
class C(B):
array = [4]
def __init__(self):
super(C, self).__init__()
print array
And when I do:
c = C()
I want it to join all their fields in the order of inheritance. And print [1, 2, 3, 4]. How can I do that?
Well when you define array in each class, it's going to override the value of the inherited class.
If you want to do this, then change your class definitions, and add the array initialisation into the __init__(self) function of each class.
I.e.
class A(object):
array = [1]
def __init__(self):
pass
class B(A):
def __init__(self):
super(B, self).__init__()
self.array += [2,3]
class C(B):
def __init__(self):
super(C, self).__init__()
self.array += [4]
print self.array
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With