【问题标题】:Calling to staticmethod from class __init__() causing "takes 1 positional argument but 2 were given" TypeError从类 __init__() 调用 staticmethod 导致“需要 1 个位置参数但给出了 2 个”TypeError
【发布时间】:2020-10-04 09:23:51
【问题描述】:

我用静态方法创建了一个类:

class DetfileDetector(Detector):
    def __init__(self, file_path, **kwargs):
        super().__init__(**kwargs)
        self.detections = self.parse_detfile(file_path)

    @staticmethod
    def parse_detfile(file_path):
        #do somthing with file_path

当我在没有实例化类的情况下调用 parse_defile 时,我得到了预期的结果

DetfileDetector.parse_detfile('foo.txt')

但是,当我从 init() 函数调用它时,我得到了错误:

TypeError: parse_detfile() 需要 1 个位置参数,但 2 个是 给定

我假设意思是'self'也传递给了staticmethod,但这是一个staticmethod,因此我希望self不会被传递。

我的修复尝试是将“file_path”设置为可选参数,默认为 None,但我得到了一个 TypeEroor

TypeError: parse_detfile() 为参数 'file_path' 获得了多个值

如果有人能告诉我如何以正确的方式编写它,我将不胜感激。

编辑:感谢您的回答,添加更多信息:

  • 我现在看到,当我从 VScode(版本 1.50.0-insider)的“python 交互式 shell”运行它时它会失败,但是从命令行运行它或没有交互式 shell 时它会通过。我会将此问题报告给 VScode 开发人员。
  • 我使用的是 python 3.7.7

Edit2:Detector类的内容

class Detector:
    def __init__(self, **kwargs):
        self.every = kwargs.pop('every', 1)
        self.min_confidence = kwargs.pop('min_confidence', .8)
        self.min_wh = kwargs.pop('min_wh', 5)
        self.min_ar = kwargs.pop('min_ar', .1)
        self.sensor_noise_cov = np.square(np.diag(kwargs.pop('sensor_noise', [10, 10, 1, 10])))
        self.classes = object_classes(kwargs.pop('cars_only', True))

【问题讨论】:

  • 我无法重现您的错误。它的工作原理如下面我的回答所示。
  • 可以分享Detector类的内容吗?
  • @PraysonW.Daniel 我用Detector编辑了这个问题

标签: python visual-studio-code static-methods


【解决方案1】:

您的代码实际上是正确

我用 Python 3.8 对其进行了测试,没有发现任何问题。

您可以使用该类或该类的实例调用静态方法。

请看这里:https://docs.python.org/3/library/functions.html#staticmethod

即使在旧版本的 Python 中,它也是如此,我一直检查到 Python 2.7。 所以你为什么会收到这个错误有点神秘。

也许可以提供更多关于您的代码的上下文?

【讨论】:

  • 这也是我的建议。问题不在此处提供的代码中
  • @PraysonW.Daniel 我们意见一致。我很好奇问题出在哪里。
  • 我也想知道。也许Detector 里有什么?
  • 感谢 Prayson W. Daniel 和 Hocli 的回答,我编辑了问题并提供了更多信息
【解决方案2】:

我认为问题不在于您显示的代码。在 Python 3.6 上测试,这工作:

class Detector:
    pass

class DetfileDetector(Detector):
    def __init__(self, file_path, **kwargs):
        super().__init__(**kwargs)
        self.detections = self.parse_detfile(file_path)

    @staticmethod
    def parse_detfile(file_path):
        #do somthing with file_path
        print(file_path)


DetfileDetector('get.txt')
# get.txt

DetfileDetector.parse_detfile('tex.txt')
# tex.txt

在 VSCode 中,在 initparse_detfile 的代码中添加断点,然后在调试模式下运行代码以查看变量。

所以检查Detector 看看是否存在问题。

【讨论】:

    【解决方案3】:

    将您的代码更改为此,它应该可以工作

    class DetfileDetector(Detector):
        def __init__(self, file_path, **kwargs):
            super().__init__(**kwargs)
            self.detections = DetfileDetector.parse_detfile(file_path)
    
        @staticmethod
        def parse_detfile(file_path):
            #do somthing with file_path
    

    【讨论】:

    • 最好使用self.detections = self.parse_detfile(file_path),因为它不需要将类的名称硬编码到代码中(如果包含类的名称发生变化,也不需要修改)。
    【解决方案4】:

    我假设意思是'self'也传递给了staticmethod,但这是一个staticmethod,因此我希望self不会被传递。

    正确。

    staticmethod 的一半意思是您不需要实例。但是,它仍然是类的一个属性,而不是顶级函数,所以:

    如果有人能告诉我如何以正确的方式编写它,我将不胜感激。

    你需要在类中寻找它,因此:

    def __init__(self, file_path, **kwargs):
        super().__init__(**kwargs)
        self.detections = DetfileDetector.parse_detfile(file_path)
    

    是的,可以说有点尴尬。但是staticmethod 的另一半要点是您实际上以这种方式将方法与类相关联。如果这看起来没有什么好处,您可以考虑在类之外创建一个普通的顶级函数。

    【讨论】:

      猜你喜欢
      • 2017-04-22
      • 1970-01-01
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 1970-01-01
      • 2020-01-19
      相关资源
      最近更新 更多