I have the following class. Every instance of the class that I create is having the same copy of the list field
class Vertex:
label = None
data = None
connected_vertices_ids = []
def __init__(self, data):
self.data = data
self.label = randint(1, 100)
def __init__(self, vertex_id, data):
self.data = data
self.label = vertex_id
def print_vertex(self):
print(self.label, self.connected_vertices_ids)
Usage
arr = []
arr.append(Vertex('Tampa', None))
arr.append(Vertex('Clearwater', None))
arr.append(Vertex('St. Pete', None))
arr.append(Vertex('Largo', None))
arr.append(Vertex('Seminole', None))
arr.append(Vertex('Orlando', None))
for vertex in arr:
print(id(vertex), id(vertex.connected_vertices_ids))
Result
140582021542688 140582025347656
140582021542744 140582025347656
140582021542800 140582025347656
140582021542856 140582025347656
140582021542912 140582025347656
140582021542968 140582025347656