【问题标题】:Strange mismatch of Python arguments [closed]Python参数的奇怪不匹配[关闭]
【发布时间】:2020-05-17 23:56:42
【问题描述】:

在测试一些代码时,我得到了一个错误:

TypeError:breadth_first_search() 采用 2 到 3 个位置参数,但给出了 4 个

函数声明的参数位如下:

def breadth_first_search(self, id: int, level=None)

调用如下所示:

tree.breadth_first_search(parent_id)

据我所知,这应该是正确的。我不知道为什么它将我的一个论点(或两个,包括self)解释为四个。有什么我遗漏的地方吗?

--

为了完整起见,这里是回溯:

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    tree = FeatureQuery.load_feature_tree("general", inventory)
  File "D:\Speechcraft\Python\core\ling_query.py", line 201, in load_feature_tree
    FeatureQuery.load_feature_node_recursive(feature_inventory, tree, results, l)
  File "D:\Speechcraft\Python\core\ling_query.py", line 221, in load_feature_node_recursive
    parent = tree.breadth_first_search(parent_id)
  File "D:\Speechcraft\Python\core\phonological_units.py", line 37, in breadth_first_search
    return self.breadth_first_search(self, id, next_level)
TypeError: breadth_first_search() takes from 2 to 3 positional arguments but 4 were given

【问题讨论】:

  • tree.breadth_first_search(parent_id) 不是您应该关注的电话。您应该在文件D:\Speechcraft\Python\core\phonological_units.py 的第37 行查看return self.breadth_first_search(self, id, next_level),其中有一个额外的self
  • 啊,就是这样,我真傻。这是我第一次尝试 Python 递归,所以我一定会绊倒一些东西。我会发布答案。

标签: python parameter-passing


【解决方案1】:

结果证明这是一个愚蠢的错误。我不小心在递归调用中包含了 self 作为参数。谢谢user2357112 supports monica

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 2022-10-07
    相关资源
    最近更新 更多