1

I was coding one day and this issue was affecting me. I understand that there can be default values for parameters and that instance variables are declared in a class using self. What I want is to instance a class multiple times and append ceirtain value to a default list parameter in the constructor, just like this:

class B(object):
    def __init__(self, item_list=[0]):
        self._item_list = item_list

class A(object):
    def __init__(self, int_list=[]):
        for item in int_list:
            b = B()
            b.item_list.append(item)
            print b.item_list

A(int_list=[1,2,3,4])

This printed out:

[0, 1]
[0, 1, 2]
[0, 1, 2, 3]
[0, 1, 2, 3, 4]

I was expecting:

[0, 1]
[0, 2]
[0, 3]
[0, 4]

Can anyone help me figure this out? I'm using Python 2.7.3

gbriones.gdl
  • 516
  • 7
  • 23

0 Answers0