Why does this error say I'm giving the function two arguments?
我定义了一个图形类,它有一个获取节点名并将名称和节点添加到字典的函数,当我运行程序时,会在底部收到错误。为什么它认为我给它两个论点?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | class Graph: def __init__(self): self.node_list = {} self.number = 0 def node(node_name): if node_name in self.node_list.keys: return node_list[node_name] else: node_list[node_name] = Node() ... def PrefixTrieConstruction(patterns): trie = Graph() trie.node('root') for pattern in patterns: currentNode = trie.node('root') for symbol in pattern: for daughter in currentNode.daughters: if daughter.label == symbol: currentNode = daughter break else: node_name = Trie.name_node() Trie.node(node_name) Trie.edge(currentNode, node_name, symbol) currentNode = node_name return Trie Traceback (most recent call last): File"PythonProject2.3.py", line 168, in <module> main() File"PythonProject2.3.py", line 163, in main TrieMatching(text, PrefixTrieConstruction(patterns)) File"PythonProject2.3.py", line 68, in PrefixTrieConstruction trie.node('root') TypeError: node() takes 1 positional argument but 2 were given |
您丢失了
1 | def node(self,node_name): |
类方法和实例方法的区别
在python中staticmethod和classmethod有什么区别
在python中,self的目的是什么
这很容易!函数
1 | def node(self,node_name): |
在Python中,方法的第一个参数始终是调用该方法的对象。按照惯例,这个变量被称为
1 | def node(node_name): |
1 | trie.node() #within this call, trie is bound to node_name |
所以当你写:
1 | trie.node('root') |
要获得预期的行为,只需声明
1 | def node(self, node_name): |