@abstractmethod
a class with an abstract method cannot be instantiated (that is, we cannot create an instance by calling it) unless all of its abstract methods have been defined in subclasses. Although this requires more code and extra knowledge, the potential advantage of this approach is that errors for missing methods are issued when we attempt to make an instance of the class, not later when we try to call a missing method. This feature may also be used to define an expected interface, automatically verified in client classes.
在父類中使用@abstractmethod抽象類
子類中必須明確定義這個abstract method才能實例化,否則會報錯。
使用@abstractmethod的優點是,避免子類在編程過程中漏掉必要的method。
class A(object):
@abstractmethod
def b(self):
raise NotImplementedError
class B(A):
def b(self):
print("在子類中不重寫我會報錯")