我想实现动态调用一个函数,生成一个docstring并应用于我需要的类方法。
目前有以下几点
class MyClass(BaseHandler, ABC):
def initialize(self, *args, **kwargs):
super(MyClass, self).initialize(*args, **kwargs)
self.description = 'My Class'
@authenticated()
@coroutine
def get(self):
"""
"""
# some code here
self.write({
'success': True,
'data': self.description
})
@authenticated()
@coroutine
def post(self):
"""
"""
# some code here
self.write({
'success': True,
'data': self.description
})
在每个方法中,我都有一个硬编码的docstring,可以很好地工作,但是我想通过一个函数来实现插入docstring。
期望结果
def generate_docstring(name, method):
docstring = """
""" % (method, name)
return docstring
class MyClass(BaseHandler, ABC):
def initialize(self, *args, **kwargs):
super(MyClass, self).initialize(*args, **kwargs)
self.description = 'My Class'
@authenticated()
@coroutine
def get(self):
# but this way does not work
self.generate_docstring(name=self.description, staticmethod='GET')
# some code here
self.write({
'success': True,
'data': self.description
})
@authenticated()
@coroutine
def post(self):
# but this way does not work
self.generate_docstring(name=self.description, staticmethod='POST')
# some code here
self.write({
'success': True,
'data': self.description
})
有没有办法在每个方法下使用函数和调用并生成docstring?