【发布时间】:2020-02-07 04:48:31
【问题描述】:
我已将超类定义为其他开发人员将编写子类来实现的正式接口,但是方法的参数会因实现而异:
class FormalInterface:
""" Subclasses will have varying arguments for formal_method """
def formal_method(*args, required_arg=0):
raise NotImplemented('Implement me')
def get_arguments():
""" Tells the user what arguments need to be passed """
raise NotImplemented('Implement me')
class MyImplementationOf(FormalInterface):
def formal_method(concrete_arg1, conrete_arg2, required_arg=0)
# impelementation...
def get_arguments():
return 'concrete_arg1', 'concrete_arg2', 'required_arg'
这是合法的 Python 代码,但是关于重新定义函数签名的警告比比皆是。
在这种情况下我是否可以忽略警告?还是我应该考虑一种更 Pythonic 的方法?
【问题讨论】:
-
您看到了什么警告?上面的运行对我没有任何警告。
-
警告来自 PyCharm 和 PyLint 等代码检查工具,当我覆盖这些警告时,我想确保我可以证明覆盖是合理的,但我不确定在这种情况下我能做到。
标签: python interface signature super subclassing