Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining fields values

Tags:

python

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?

like image 238
bobby Avatar asked Jul 25 '26 19:07

bobby


1 Answers

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
like image 126
will Avatar answered Jul 28 '26 08:07

will