3

When I run this code:

class corners():

    def __init__(self, odw=0, connections=[]):
        self.odw = odw
        self.connections = connections

w = [corners() for i in range(9)]


print w[1].odw, w[2].odw, w[3].odw
print w[1].connections, w[2].connections, w[3].connections

w[1].odw = 1
w[3].odw = 1
w[1].connections.append(2)
w[1].connections.append(3)
w[2].connections.append(88)

print w[1].odw, w[2].odw, w[3].odw
print w[1].connections, w[2].connections, w[3].connections

The answer is:

0 0 0
[] [] []
1 0 1
[2, 3, 88] [2, 3, 88] [2, 3, 88] // it should be [2, 3] [88] []

The answer for the attribute 'odw' is correct. BUT: I don't know why the attribute 'connections' is modificated for all objects, when I change only specific. For example, when I add new element to the list w[1] it is added for all others. Why?

Thanks for help.

Lipstick
  • 739
  • 3
  • 8
  • 14

0 Answers0