How do I expand a list from a parent class?

I have the following code. The parent class has a list of elements that children should add.

Every parent instance must have this list and every child must have this list + additional values.

class Parent(object):
    a_list = ['parent_item1', 'parent_item2', ]

    def print_list(self):
        print(self.a_list)


class Child1(Parent):
    def __init__(self, *args, **kwargs):
        super(Child1, self).__init__(*args, **kwargs)
        self.a_list += ['child1_item']


class Child2(Parent):
    def __init__(self, *args, **kwargs):
        super(Child2, self).__init__(*args, **kwargs)
        self.a_list += ['child2_item']


parent = Parent()
child1 = Child1()
child2 = Child2()

parent.print_list()
    # >> ['parent_item1', 'parent_item2', 'child1_item', 'child2_item']
child1.print_list()
    # >> ['parent_item1', 'parent_item2', 'child1_item', 'child2_item']
child2.print_list()
    # >> ['parent_item1', 'parent_item2', 'child1_item', 'child2_item']

      

How can I get the following result?

['parent_item1', 'parent_item2', ]
['parent_item1', 'parent_item2', 'child1_item', ]
['parent_item1', 'parent_item2', 'child2_item', ]

      

+3


source to share


3 answers


list += other_list

expand the list locally . Use an operator +

that returns a new list:

class Parent(object):
    a_list = ['parent_item1', 'parent_item2', ]
    def print_list(self):
        print(self.a_list)

class Child1(Parent):
    def __init__(self, *args, **kwargs):
        super(Child1, self).__init__(*args, **kwargs)
        self.a_list = self.a_list + ['child1_item']  # <-------

class Child2(Parent):
    def __init__(self, *args, **kwargs):
        super(Child2, self).__init__(*args, **kwargs)
        self.a_list = self.a_list + ['child2_item']  # <-------


parent = Parent()
child1 = Child1()
child2 = Child2()
parent.print_list()
child1.print_list()
child2.print_list()

      

output:

['parent_item1', 'parent_item2']
['parent_item1', 'parent_item2', 'child1_item']
['parent_item1', 'parent_item2', 'child2_item']

      




Alternatively, you can make a copy of the parent class list:

...
self.a_list = self.a_list[:]
self.a_list += ['child1_item']

      

+3


source


You want to have a constructor for Parent

. The problem is that they all use the same list that is created when they are declared Parent

. Instead, you want:

class Parent(object):
    def __init__(self):
        self.a_list = ['parent_item1', 'parent_item2', ]
    def print_list(self):
        print(self.a_list)

      



This way, whenever you create a parent, you create a new list containing ['parent_item1', 'parent_item2', ]

.

0


source


I solved this, the solution was very simple and clean. No need for super, or init ()

class Parent(object):
    a_list = ['parent_item1', 'parent_item2', ]

    def print_list(self):
        print(self.a_list)


class Child1(Parent):
    a_list = Parent.a_list + ['child1_item', ]


class Child2(Parent):
    a_list = Parent.a_list + ['child2_item', ]


parent = Parent()
child1 = Child1()
child2 = Child2()

parent.print_list()
    # >> ['parent_item1', 'parent_item2', ]
child1.print_list()
    # >> ['parent_item1', 'parent_item2', 'child1_item', ]
child2.print_list()
    # >> ['parent_item1', 'parent_item2', 'child2_item']

      

0


source







All Articles