Python - 在子类方法定义中指定精确参数

标签 python python-3.x inheritance warnings variadic-functions

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/

相关文章:

python - Windows 上的 ipdb/set_trace() 错误?

python - 基于socket的简单python聊天程序

python - 如何将 python tkinter 窗口嵌入到浏览器中?

Python 在广泛的处理程序中捕获先前的异常

php - Doctrine 2 索引继承

Java 无法调用 addAll() 将子类类型的数组列表添加到 <?扩展基类>

python - 如何更新 Python 包?

python-3.x - Sqlalchemy 从关系中获取数据

python - 功能在 "return(False)"后不会停止

接口(interface)中的构造函数方法? (在戈兰语中)