-2
這裏是我的代碼:Python沒有返回相同的對象?
class Node:
nodes = {}
def __init__(self, name):
self.name = name
self.parent = []
self.children = []
Node.nodes[self.name] = self
def addParent(self, parent):
print "adding parent %s for %s " % (parent, self.name)
self.parent.append(parent)
print self.parent
def addChild(self, child):
self.children.append(child)
def removeParent(self, parent):
try:
self.parent.remove(parent)
except:
pass
def removeChild(self, child):
try:
self.children.remove(child)
except:
pass
def lookup(obj):
print "calling look up"
Node.nodes.get(obj)
def create_node(obj):
return lookup(obj) or Node(obj)
# Tree has Nodes
class Tree:
trees = {}
def __init__(self, name, root):
self.name = name
self.root = root
self.size = 1
self.nodes = set() # tree has unique nodes
self.nodes.add(root)
Tree.trees[self.name] = self
def addNode(self, node):
self.nodes.add(node)
self.size += 1
def removeNode(self, node):
try:
self.nodes.remove(node)
except:
return
self.size -= 1
def setRoot(self, root):
self.root = root
def print_tree(self):
for i in self.nodes:
if i == self.root.name:
print "root: %s" % i
else:
print i
def main():
roota = create_node("a")
ta = Tree("a", roota)
childaa = create_node("a_a")
roota.addChild(childaa)
childaa.addParent(roota)
ta.addNode(childaa)
childab = create_node("a_b")
roota.addChild(childab)
childab.addParent(roota)
ta.addNode(childab)
# make one of the child of a root
rootb = create_node("a_a") # should give me a node that already exists from the above tree
tb = Tree("a_a", rootb)
childbb = create_node("a_b") # this node should have two parents now, a and a_a
rootb.addChild(childbb)
childbb.addParent(rootb)
tb.addNode(childbb)
for node in Node.nodes.itervalues():
print "Name: %s" % node.name
if node.parent:
print "Parent: %s" % [parent.name for parent in node.parent]
else:
print "Parent: %s" % node.parent
print "Children: ", [node.name for node in node.children]
print ""
if __name__ == '__main__':
main()
和腳本的輸出:
Name: a
Parent: []
Children: ['a_a', 'a_b']
Name: a_a
Parent: []
Children: ['a_b']
Name: a_b
Parent: ['a_a']
Children: []
a_a
應該有父a
。第80行添加a
作爲父項a_a
a_b
應該有父項a_a
和a
。 85行是加a
作爲父母的a_b
有人可以向我解釋爲什麼這不是在這種情況下的代碼?
和所需的腳本的輸出:
Name: a
Parent: []
Children: ['a_a', 'a_b']
Name: a_a
Parent: ['a']
Children: ['a_b']
Name: a_b
Parent: ['a', 'a_a']
Children: []
請不要使用'except:pass' * ever *。它確實吞下了包括SystemExit和KeyboardInterrupt在內的所有內容。如果你真的想忽略所有的實際*異常*,使用'except Exception:pass'。但即使這是一個糟糕的主意 - 「ValueError」就是你需要抓住的所有東西 – ThiefMaster
你試圖弄清楚這個bug,你採取了哪些步驟?我們可以通過代碼尋找並發現錯誤,但如果你學會了如何自己找到它,它會更好。 –
@WinstonEwert只是在添加子/父的每一步打印。我看到Node obj的len是三個,所以它似乎在執行查找,所以當a_a被查找時,它應該給我一個已經具有'a'作爲父節點的節點,但它並不是我在這裏問的。 – ealeon