【发布时间】:2019-08-27 00:35:36
【问题描述】:
我有一个类(我称之为“大师类”),它使用来自多个其他类的实例方法。其他类在__init__ 上导入并存储为私有实例属性。
我想使用其他类的实例方法,具有以下属性:
- 不重写主类中的文档字符串或签名
- 让
autodoc解析来自其他类的文档字符串,就像它们是主类的文档字符串一样
目前,我的设置方式:
class OtherClass:
"""Some other class that I import."""
def __init__(self):
pass
def print_num(self, num: float = 15) -> None:
"""Print a num.
Args:
num: Number to print
"""
print(num)
from .other import OtherClass
class MasterClass:
def __init__(self, other_class: OtherClass):
"""Create master class with a bunch of other classes.
For simplicity, I only included one class here as an arg.
Args:
other_class: Houses some methods
"""
self._other_class = other_class
def print_num(self, num: float = 15):
"""Print a num.
Args:
num: Number to print
"""
self._other_class.print_num(num)
要加入OtherClass.print_num,我必须:
- 手动复制签名
- 手动复制文档字符串(因为我希望我的 Sphinx 文档准确无误)
- 在
MasterClass的方法中调用OtherClass的方法,手动传入所有的args和kwargs
有没有更好的方法来做到这一点?
提前感谢您的指导。
【问题讨论】:
标签: python class methods attributes docstring