gpt4 book ai didi

python - 继承时更改文档字符串但保留方法相同

转载 作者:太空宇宙 更新时间:2023-11-03 10:57:33 24 4
gpt4 key购买 nike

我正在构建一个 HTTP API,并将大量代码分解到一个父类(super class)中,该父类(super class)处理对一组对象的请求。在我的子类中,我指定操作应该在哪些数据库模型上进行,父类(super class)负责其余部分。

这意味着我不需要从父类(super class)中重新实现 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

具体来说,我需要 ActivityListApigetpostCollectionApi 一样运行,但我想要不同的文档字符串(为了自动生成文档)。

我能做到:

    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()

关于python - 继承时更改文档字符串但保留方法相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38965232/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com