考虑以下代码示例:class BaseExtractor(object, metaclass=abc.ABCMeta): @abc.abstractmethod def extract(self, interval, *args, **kwargs): raise NotImplementedErrorclass ChildExtractor(BaseExtractor): def extract(self, interval, variants, anchor, fixed_len=True, **kwargs): pass为什么 PyCharm 告诉我ChildExtractor.extract()不匹配BaseExtractor.extract()?我开始认为我只是犯了一些非常愚蠢的错误......编辑:class ChildExtractor(BaseExtractor): def extract(self, interval, variants, anchor, *args, fixed_len=True, **kwargs): pass这会导致相同的错误。
1 回答
慕哥6287543
TA贡献1831条经验 获得超10个赞
考虑以下用法BaseExtractor:
def extractor_usage(extractor: BaseExtractor):
extractor.extract(interval=10)
它正确使用BaseExtractor接口,但传递ChildExtractor给此方法会导致TypeError.
这个问题可以通过 makevariants和anchoroptional 来解决,例如:
class ChildExtractor(BaseExtractor):
def extract(self, interval, variants=None, anchor=None, *args, fixed_len=True, **kwargs):
pass
extractor_usage(ChildExtractor()) # No TypeError here
添加回答
举报
0/150
提交
取消