我正在制作一个 python 脚本来从 sqlite db 中提取数据以获取网络路径。我有两个类:节点,链接:
class node:
nodeName = str()
nodeType = str()
node1 = str()
node2 = str()
active = 0
def __init__(self, nodeID):
self.nodeID = nodeID
cursor = network.execute("SELECT linkID FROM link WHERE nodeA = ? OR nodeB = ?", (nodeID, nodeID,))
row1 = cursor.fetchone()
row2 = cursor.fetchone()
if row1:
self.link1 = link(row1[0])
if row2:
self.link2 = link(row2[0])
self.nodeName, selfsnodeType, self.active = network.execute("SELECT nodeName, nodeType, active FROM node WHERE nodeID = ?", (nodeID,)).fetchall()[0]
Link 完全一样,只是它尝试获取两个节点子节点。如您所见,这创建了一个循环引用,但似乎有三层深,链接子节点没有获得自己的节点子节点,节点子节点也没有链接子节点。尝试读取值返回 None 类型:
NODE object 67 getting link children:
(19,)
(48,)
link object 19 getting node childs:
(67,)
(112,)
NODE object 67 getting link children:
None
None
- 这种行为是设计使然吗?
- 这是一个坏主意吗?
- 是否可以遍历孩子?