gpt4 book ai didi

python - 从集合继承。计数器: 'fromkeys' is abstract

转载 作者:太空宇宙 更新时间:2023-11-03 15:07:16 24 4
gpt4 key购买 nike

我有一个继承自collections.Counter的Python类:

class Analyzer(collections.Counter):
pass

当我在这段代码上使用 pylint 时,它的答案是:

W: Method 'fromkeys' is abstract in class 'Counter' but is not overridden (abstract-method)

我在我的机器上检查了 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 本身没有......

在这种情况下如何解决这个警告?

最佳答案

This question应该在这里回答一些问题。基本上,pylint 检查引发的 NotImplementedError 异常,以确定方法是否是抽象的(在本例中为误报)。添加注释 #pylint:disable=W0223 将禁用此检查。

this question中也提出了类似的问题.

关于python - 从集合继承。计数器: 'fromkeys' is abstract,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44544806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com