我试图将一个方法实现为一个抽象方法和一个类方法,但这样做并没有获得抽象类的任何好处。你知道吗
例如:
from abc import ABC, abstractmethod
class BasePipeline(ABC):
@classmethod
@abstractmethod
def consume_frame(cls):
pass
@abstractmethod
def consume_frame_two(self):
pass
class AnotherSubclass(BasePipeline):
@classmethod
def does_nothing(cls):
a = 1 + 1
# Call it.
AnotherSubclass.consume_frame()
这不会引发任何异常,也不会出错。我希望它能说一些类似于:consume_frame_two is not implemented
和consume_frame is not implemented
的话。你知道吗
不确定我的行为是什么,或者我只是做错了什么。如果AnotherSubclass.consume_frame()
没有正确地实现为类方法,我希望它引发一个异常。你知道吗
您的代码没有尝试创建
AnotherSubclass
类的实例。它所做的只是访问标记为抽象的classmethod
的实现。Python的ABC抽象类并不打算阻止这种访问。你知道吗abc
模块旨在帮助您定义一个协议或接口,一个基类,它设置了对应该被视为相同的具体对象上必须存在哪些属性的期望。你知道吗为此,使用} documentation :
ABC
子类所能做的就是防止在类层次结构中创建至少具有一个abstractmethod
或abstractproperty
属性的任何类的实例。从^{任何
abstractmethod
修饰的方法仍然可以调用;没有任何机制可以阻止这种情况,而具体实现可以使用super().name()
访问abstractmethod
对象的实现实际上是模块的一个特定目标。来自同一来源:以及
该类的任何其他属性的使用方式与其他类相同,包括
classmethod
对象。你知道吗在封面下,每个
ABCMeta
元类为您创建的每个类提供了一个__abstractmethods__
属性,它是一个frozenset
对象,该对象的__isabstractmethod__
属性设置为True
的类上的任何属性的名称,子类只需使用与父抽象方法对象相同的名称,将其设置为__isabstractmethod__
未设置为true的属性可从该类的集合中删除该名称。当您试图创建一个类的实例,而该类的__abstractmethods__
不是空的时,Python将引发一个异常。你知道吗如果您需要进一步锁定您的类定义,那么您必须提出我们自己的元类或其他机制来实现这些规则。例如,您可以将
classobject
属性包装到自己的descriptor object中,以防止调用绑定到具有非空__abstractmethods__
属性的类的classmethod。你知道吗相关问题 更多 >
编程相关推荐