I have this python code:
class Node:
def __init__(self, label, adjacencies=[]):
self._adjacencies = adjacencies
self._label = label
And, somewhere later I wrote this:
def addAdjacency(self, node):
print("adding ", node.getLabel(), " to ", self._label)
if node in self._adjacencies:
return
self._adjacencies.append(node)
node.addAdjacencies(self)
My idea is that I can do:
a = Node('a')
b = Node('b')
a.addAdjacency(b)
And that this will result in a having b and b having a in their own adjacencies list. Instead if I run:
if a.getAdjacencies() is b.getAdjacencies():
print("BUT WHY?")
I get the string printed, and the a adjacencies list contains 'a' itself (added by b), so it seems the list is shared by the same objects, but I don't understand why, can someone explain? I found that removing the optional argument solved the issue, so it must be the problem. What am I missing?