我试图用一个名称和它的邻居的dict,以及它引用这个邻居(不一定是邻居的名称)的标记来创建一个节点类
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
但是,当我执行以下操作时:
foo = node(name='foo')
foo.addNeighbor('fooneighbor',node(name='bar'))
dict条目{'foonneighbor',node object}也出现在foo.neighbors['foonneighbor'].neighbors中(我希望它是空的)
我猜这与dicts的工作方式有关,但我并不知道。有人能开导我吗
引用docs
所以,你可以像这样解决你的问题
相关问题 更多 >
编程相关推荐