我正在构建一个HTTP API,并将大量代码分解到一个超类中,该超类处理对对象集合的请求。在我的子类中,我指定操作应该在哪些数据库模型上工作,而超类负责其余的工作。
这意味着我不需要从超类中重新实现get, post等方法,但是,我想在子类中更改它们的文档字符串,以便我可以拥有一些更具体于端点操作的实际模型的文档。
继承父类的功能但改变文档字符串的最干净的方法是什么?
的例子:
class CollectionApi(Resource):
"""Operate on a collection of something.
"""
class Meta(object):
model = None
schema = None
def get(self):
"""Return a list of collections.
"""
# snip
def post(self):
"""Create a new item in this collection.
"""
# snip
class ActivityListApi(CollectionApi):
"""Operations on the collection of Activities.
"""
class Meta(object):
model = models.Activity
schema = schemas.ActivitySchema
具体来说,我需要ActivityListApi
有get
和post
运行像在CollectionApi
,但我想要不同的文档字符串(为了自动文档的缘故)。
我可以这样做:
def get(self):
"""More detailed docs
"""
return super(ActivityListApi, self).get()
但是这看起来很乱
class CollectionApi(Resource):
"""Operate on a collection of something.
"""
def _get(self):
"""actual work... lotsa techy doc here!
the get methods only serve to have something to hang
their user docstrings onto
"""
pass
def get(self):
"""user-intended doc for CollectionApi"""
return self._get()
class ActivityListApi(CollectionApi):
def get(self):
"""user-intended doc for ActivityListApi"""
return self._get()