Trie是节点列表:
class node:
def __init__(self, parent, daughters, edge):
self.parent = parent
self.daughters = daughters
self.edge = edge
trie.append(self)
self.index = len(trie) - 1
def TrieConstruction(patterns):
global trie
for node in trie:
print('Node: ', node.parent, node.daughters, node.edge, node.index)
trie.append(node(0, [], 0))
...
程序会给出以下错误:
File "trieMatching.1.py", line 22, in TrieConstruction
trie.append(node(0, [], 0))
UnboundLocalError: local variable 'node' referenced before assignment
我不知道为什么这个变量会被取消赋值;难道函数不明白node是一个类,我正在实例化它吗?你知道吗
您正在为类和
for
循环目标使用名称node
:如果
trie
为空,则node
将在函数中保持未绑定状态,因为没有可分配的值。你知道吗你得重新命名其中一个。我建议您遵循Python style guide并使用CamelCase作为类名:
小写字母\u加下划线表示您的函数(以及更好地反映职责的名称):
相关问题 更多 >
编程相关推荐