我理解下面的代码为什么抛出和异常,以及一些避免异常的方法,但我不理解使用@abstractmethod来创建抽象接口的预期方法
我的目标是
问题是,如果我在构造函数中添加对“super”的适当调用,我最终将调用abstractmethod并引发异常
我觉得我想得不对。什么是Python的方式
from abc import ABC, abstractmethod
class Foo(ABC):
@abstractmethod
def __init__(self, my_arg):
raise NotImplementedError
class FooAdapter(Foo):
def __init__(self, my_arg):
super().__init__()
根据下面的公认答案
您根本不需要使用NotImplementedError,只需使用“pass”。 如果子类未实现指定的方法,@abstractmethod机制将引发异常
从
abc.abstarctmethod
{a1}因此,您只需要将存根放在不做任何事情的位置,而不是引发
NotImlementedError
相关问题 更多 >
编程相关推荐