class Node:
def __init__(self,parent = None):
self.parent = parent
self.children = []
def AddNode(self):
self.children.append(Node(self))
def getIndex(self):
return self.parent.children.index(self)
a = Node()
b = a.AddNode()
print b.getIndex()
在上面这样的对象树中,子对象在父对象的子对象中查找索引的最佳方法是什么?我使用的是self.parent.children.index(self)
,但这似乎有点扭曲。有更好的方法吗?在
一点:这不太管用,因为AddNode不返回任何内容。 除此之外,你所做的一切都很好。只要您在按需(惰性)检索索引,这是一种直接的方法。如果您想要更直接的东西,我建议您在AddNode中链接子节点时存储索引。在
输出(booooorrriningg):
^{pr2}$相关问题 更多 >
编程相关推荐