You can see, that i've created two instances of class A. So, a.d (dict of first instance) and b.d (dict of second instance) have to be different! But they are not, we clearly can see that a.d == b.d = True. Okay, so, it should mean that if i will modivy a.d, then b.d will be modifyed too, right? No. Now they are diffrent. And you will say, okay, they just compare together by value, not by it's link value. But i have another trouble with code:
class cluster(object):
def __init__(self, id, points):
self.id = id
self.points = points
self.distances = dict() # maps ["other_cluster"] = distance_to_other_cluster
def set_distance_to_cluster(self, other_cluster, distance):
"""Sets distance to itself and to other cluster"""
assert self.distances != other_cluster.distances
self.distances[other_cluster] = distance
other_cluster.distances[self] = distance
and at the end i'm getting the same "distances" dict object for all clusters. Am i do sth wrong? :'(
Dictionaries are not regular data types, you have to be very careful when working with them.
a = [5]
b = a
b.append(3)
You'd think that with this code that a=[5]
and b=[5,3]
but in reality they BOTH equal [5, 3]
.
And by the way when you assigned the value a.d["A"] = 1
you turned the ARRAY
into a DICTIONARY
, dictionaries don't have the problem above so it didn't come up again.
Solution is to use dictionaries from the start since it suit your data type anyways.