The code below return this:
Person A have friends ['B', 'A']
Person B have friends ['B', 'A']
But it is intended to return:
Person A have friends ['B']
Person B have friends ['A']
"A" must reference "B" and vice-versa. But "A" is referencing "B" and "A", why?
CODE:
class person:
name = ""
friends = set()
def __init__(self, name):
self.name = name
def add(self, new):
self.friends |= {new}
new.friends |= {self}
#---------------------------------------------------------------------------
pa = person("A")
pb = person("B")
pa.add(pb)
print( "Person", pa.name, "have friends", [f.name for f in pa.friends] )
print( "Person", pb.name, "have friends", [f.name for f in pb.friends] )