3

如何在 Python 父类中指定某些字段/方法需要在子类中覆盖?

4

2 回答 2

5

你可以提出一个NotImplementedError

def my_method(self, arg):
    raise NotImplementedError('Implement me')

对于属性,您可以使用@property装饰器:

@property
def my_property(self):
    raise NotImplementedError('Implement me as well')
于 2013-02-05T09:44:28.533 回答
1

您可以查看抽象基类模块。

然而,一个更简单的替代方法是定义一个不做任何事情的存根实现,或者引发NotImplementedError.

于 2013-02-05T09:46:11.953 回答