【发布时间】:2015-07-03 08:05:53
【问题描述】:
from abc import abstractmethod, ABCMeta
class AbstractBase(object):
__metaclass__ = ABCMeta
@abstractmethod
def must_implement_this_method(self):
raise NotImplementedError()
class ConcreteClass(AbstractBase):
def extra_function(self):
print('hello')
# def must_implement_this_method(self):
# print("Concrete implementation")
d = ConcreteClass() # no error
d.extra_function()
我使用的是 Python 3.4。我想定义一个抽象基类,它定义了一些需要由它的子类实现的函数。但是当子类没有实现函数时,Python 不会引发 NotImplementedError...
【问题讨论】:
-
拨打
d.must_implement_this_method()会发生什么?
标签: python python-3.x abstract-class python-3.4