I'm trying to make a node class with a name and a dict of its neighbors, along with the tag by which it refers to this neighbor (not necessarily the neighbor's name).
class node:
def __init__(self,name='',neighbors={}):
self.name=name
self.neighbors=neighbors
def addNeighbor(self,neighborTag,neighbor):
if neighborTag not in self.neighbors.keys():
self.neighbors[neighborTag] = neighbor
However when I perform the following:
foo = node(name='foo')
foo.addNeighbor('fooneighbor',node(name='bar'))
The dict entry {'fooneighbor',node object} also appears in foo.neighbors['fooneighbor'].neighbors (which I expected to be blank).
I'm guessing this is something to do with how dicts work that I'm not aware of. Can anyone enlighten me?