请帮助理解如何指定基类方法签名,该签名至少可以包含一个可能的附加参数
我需要有一个forward
方法,该方法至少接受X,以及根据子类可能不需要的附加参数
class Layer:
def forward(self, X, *args) -> float:
子类SoftmaxWithLogLossforward
有X
和一个附加参数T
class SoftmaxWithLogLoss(Layer):
def forward(self, X: np.ndarray, T: np.ndarray) -> float: <--- Signature does not match
它会导致警告Signature of method 'SoftmaxWithLogLoss.forward()' does not match signature of base method in class 'Layer'
请解释原因以及如何修复
我想是mypy引起了这个错误。 原因是违反了Liskov替代原则https://en.wikipedia.org/wiki/Liskov_substitution_principle
所有基类对象都必须可替换为其子类对象。但是,由于子类中方法的签名不同,因此无法执行此操作
你可以在这里找到更多 Mypy produces incompatible signature error but Liskov Substitution Principle is satisfied
相关问题 更多 >
编程相关推荐