【发布时间】:2017-06-14 12:27:21
【问题描述】:
我有一个继承自 collections.Counter 的 python 类:
class Analyzer(collections.Counter):
pass
当我在这段代码上使用 pylint 时,它的答案是:
W:方法“fromkeys”在“Counter”类中是抽象的,但未被覆盖(抽象方法)
我在我的机器上检查了collections.Counter 的实现,实际上,这个方法没有实现(评论有助于理解原因):
class Counter(dict):
...
@classmethod
def fromkeys(cls, iterable, v=None):
# There is no equivalent method for counters because setting v=1
# means that no element can have a count greater than one.
raise NotImplementedError(
'Counter.fromkeys() is undefined. Use Counter(iterable) instead.')
但是,我真的不知道如何实现这个方法,如果Counter 本身没有......
在这种情况下如何解决这个警告?
【问题讨论】:
-
忽略警告?皮林特错了,曲解了事情。
-
我的意思是,一个天真的解决方案可能是以
collections.Counter中的方式实现它。但这甚至不应该是必要的...... -
这是警告而不是错误
-
@MartijnPieters 这就是我到现在为止的处理方式......
-
@JaredGoguen 或者,不要重新实现它,而是重用泄漏的实现:
fromkeys = collections.Counter.fromkeys。但正如你所说,这甚至没有必要。
标签: python inheritance abstract python-collections