我正在尝试生成一个具有递归“printAll”方法的树。在
我的代码是:
class Node(object):
def __init__(self, children=[], tag=None):
self.children = children
self.tag = tag
def appendChild(self, child):
self.children.append(child)
def getChildren(self):
return self.children
def printAll(self):
print self.getChildren()
for child in self.children:
child.printAll()
当我运行它时,我得到这样的信息:“调用Python对象时超过了最大递归深度”。在
我猜这与调用子级的printAll()方法时将顶级作用域传递给子级有关,从而导致无限循环。任何帮助都是非常感谢的。在
尝试更改默认值
children
:您可能有一个"mutable default argument"的情况
相关问题 更多 >
编程相关推荐