使用 Anytree 时未定义名称 class

Name not defined when using Anytree class

我正在使用 Python 的 anytree 包,我正在尝试遍历树:

from anytree import Node, RenderTree, AsciiStyle

f = Node("f")
b = Node("b", parent=f)
a = Node("a", parent=b)

print(RenderTree(f, style=AsciiStyle()).by_attr())


for node in LevelOrderIter(f):
    print node.name

但是,由于某种原因,我遇到了错误 NameError: name 'LevelOrderIter' is not defined

我已经安装了 anytree(它可以创建节点等),根据语法我似乎是正确的:http://anytree.readthedocs.io/en/latest/apidoc/anytree.iterators.html#anytree.iterators.LevelOrderIter

有什么想法是行不通的吗?

完整错误:

f
+-- b
    +-- a
Traceback (most recent call last):
  File "aa.py", line 10, in <module>
    for node in LevelOrderIter(f):
NameError: name 'LevelOrderIter' is not defined

如果您从库中调用一个函数,您可以像 anytree.LevelOrderIter() 那样调用它并提及库,或者您可以像这样直接导入它:

from anytree import LevelOrderIter

正确的示例如下所示(文档将在下一个版本中修复)

from anytree import Node, RenderTree, AsciiStyle, LevelOrderIter

f = Node("f")
b = Node("b", parent=f)
a = Node("a", parent=b)

print(RenderTree(f, style=AsciiStyle()).by_attr())


for node in LevelOrderIter(f):
    print node.name