可能不太理想,但也许您可以使用装饰器来扩展文档字符串。例如:
class extend_docstring: def __init__(self, method): self.doc = method.__doc__ def __call__(self, function): if self.doc is not None: doc = function.__doc__ function.__doc__ = self.doc if doc is not None: function.__doc__ += doc return functionclass baseClass: def mymethod(myargument): """This does something Params ------ myargument : int Description of the argument """ [...]class MyClass1(baseClass): @extend_docstring(baseClass.mymethod) def mymethod(myargument): baseClass.mymethod(myargument) [...]class MyClass2(baseClass): @extend_docstring(MyClass1.mymethod) def mymethod(myargument, argument2): """argument2 : int Description of the additional argument """ baseClass.mymethod(argument) [...]print('---baseClass.mymethod---')print(baseClass.mymethod.__doc__)print('---MyClass1.mymethod---')print(MyClass1.mymethod.__doc__)print('---MyClass2.mymethod---')print(MyClass2.mymethod.__doc__)
结果:
---baseClass.mymethod---This does something Params ------ myargument : int Description of the argument---MyClass1.mymethod---This does something Params ------ myargument : int Description of the argument---MyClass2.mymethod---This does something Params ------ myargument : int Description of the argument argument2 : int Description of the additional argument
如果将装饰器作为描述符并搜索到该覆盖器,则可以动态解析覆盖方法,
__get__但这意味着装饰器不再可堆叠,因为它不返回实函数。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)