-1

Given the following sample code:

class Parent:
    some_list = []  # type: list[int]

    def __init__(self, id):
        self.id = id

if __name__ == '__main__':
    list_of_parents = []  # type: list[Parent]

    # Create 10 parents
    for i in range(10):
        list_of_parents.append(Parent(i))

    # Go over each parent and add 10 integers to each of their lists
    for i, p in enumerate(list_of_parents):
        for j in range(10):
            list_of_parents[i].some_list.append(1)

After executing that code, there will be a list of 10 Parent objects, each of them have a list with 100 entries. (consisting of 10 times integers 0-9), even though those j elements are explicitly added only to list_of_parents[i].

What's the reason for this?

user1211030
  • 2,680
  • 2
  • 19
  • 22
  • 3
    You've defined `some_list` on the class instead of the instance. Every instance shares one `some_list` object. – Moses Koledoye Jul 28 '17 at 14:21
  • 2
    Please read [this](https://stackoverflow.com/questions/68645/static-class-variables-in-python) SO post about static class variables. What is happening here is that because you defined `some_list` outside of `class methods` but inside a `class` it is treated as one single `list` for all instances of `Parent`, you should instead define `some_list` in the `__init__()` so that each `list` is unique to each instance – Professor_Joykill Jul 28 '17 at 14:23

1 Answers1

1

It's what Moses Koledoye already mentioned. Try something like this and you will see the difference:

class Parent:

    def __init__(self, id):
        self.id = id
        self.some_list = []# type: list[int]

if __name__ == '__main__':
    list_of_parents = []  # type: list[Parent]

    # Create 10 parents
    for i in range(10):
        list_of_parents.append(Parent(i))

    # Go over each parent and add 10 integers to each of their lists
    for i, p in enumerate(list_of_parents):
        for j in range(10):
            list_of_parents[i].some_list.append(1)
            print(list_of_parents[i].some_list)
            print("__________")

Output:

[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1]
__________
[1, 1]
__________
[1, 1, 1]
__________
[1, 1, 1, 1]
__________
[1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
__________
1408786user
  • 1,868
  • 1
  • 21
  • 39