我有一个抽象基类Animal
:
class Animal(metaclass=abc.ABCMeta):
@abc.abstractmethod
def move(self):
raise NotImplementedError()
@abc.abstractmethod
def eat(self):
raise NotImplementedError()
现在,我有另一个abc,它仅实现以下方法之一:
class Bird(Animal):
def move(self):
print("fly")
实现缺少方法的另一个类:
class Eagle(Bird):
def eat(self):
print("eagle eats")
但是当我有意希望它保持抽象状态时,PyCharm提示
Bird
“必须实现所有抽象方法”。我是否缺少某些东西,或者这是一个错误?如果只是错误,我可以以某种方式忽略警告(类似于
#noqa
)吗?
最佳答案
只需将Bird
标记为抽象:
from abc import ABC
class Bird(Animal, ABC):
def move(self):
print("fly")
经过一番思考后,实际上,我认为为此目的,就像您最初所做的那样,指定
metaclass=ABCMeta
会更有意义,因为从概念上讲,我们不想修改Bird
的继承层次结构,而是将其标记为抽象类(为了PyCharm的利益),也许这是一种更干净的方法。
关于python-3.x - 故意抽象的子类上的PyCharm "must implement all abstract methods",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56219651/