gpt4 book ai didi

python - "useless super delegation"的 Pylint 警告

转载 作者:行者123 更新时间:2023-12-01 23:54:29 26 4
gpt4 key购买 nike

Pylint 发出警告:对于下面的 SpecificError 类,方法“__init__”中无用的 super 委托(delegate)(useless-super-delegation)

class MyProjectExceptions(Exception):
"""The base class for all my project's exceptions."""
def __init__(self, message, func):
super(MyProjectExceptions, self).__init__(message) # Normal exception handling
self.func = func # Error origin for logging

class SpecificError(MyProjectExceptions):
"""Raise when a specific error occurs."""
def __init__(self, message, func):
super(SpecificError, self).__init__(message, func)

这里将参数传递给父类(super class)的正确方法是什么?

最佳答案

如果您调用SpecificError 类,它将查找__init__。如果它不存在,它将调用父级的 __init__。这里不需要添加__init__,因为它与父类完全相同。这基本上是代码重复。

你应该这样做:

class SpecificError(MyProjectExceptions):
"""Raise when a specific error occurs."""

关于python - "useless super delegation"的 Pylint 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45601477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com