强制覆盖字段/方法?

Al *_*ndy 3 python

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

Ble*_*der 5

你可以提出一个NotImplementedError:

def my_method(self, arg):
    raise NotImplementedError('Implement me')
Run Code Online (Sandbox Code Playgroud)

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

@property
def my_property(self):
    raise NotImplementedError('Implement me as well')
Run Code Online (Sandbox Code Playgroud)