Python 中是否有任何方法可以在父类方法中指定可变(未知)数量的参数及其子类中的实际(真实)参数,而不会收到警告?这里有一个例子让我理解(我使用的是Python 3.5):
import abc
class SuperClass(metaclass=abc.ABCMeta):
@abc.abstractmethod
def method(self, *args):
class SubClass1(SuperClass):
def method(self, one, two, three): # <-- I get a warning here
# Stuff
class SubClass2(SuperClass):
def method(self, *args):
# Stuff
我不知道如何使这段代码没有警告。我收到的警告是:
Signature of method 'SubClass1.method() does not match signature of base method in class 'SuperClass'
已更新
我仅在我的 IDE Pycharm 4.5 中收到由检查 Python->“类必须实现所有抽象方法”引起的警告。
最佳答案
你可以在子类中尝试这个:
class SubClass1(SuperClass):
def method(self, *args):
try:
one, two, three = args
except ValueError:
print('bad number of args')
话虽如此,如果您希望在 IDE 中使用代码完成功能,这可能没有帮助。
关于Python - 在子类方法定义中指定精确参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32910477/