我正在用大量的方法和长的文档字符串对一个类进行子类化。如果我调用IPython help函数,我会看到原始类的所有帮助。我猜这是意料之中的,有什么方法可以抑制它吗?我只想看到我重新定义的方法。
mymodule.py
是:
import matplotlib.axes
class MySubclass(matplotlib.axes.Axes):
pass
如果我在IPython中这样做:
import mymodule
help(mymodule)
打印输出很大,因为它包含所有“从matplotlib.axes._axes.Axes继承的方法:”,这是兆字节的文本,因为它列出了类的所有方法的文档字符串。
发布于 2018-06-07 22:09:08
正如here所讨论的,一种可能的工作解决方案是手动移除文档
## List the methods from the original class
method_list = [func for func in dir(mymodule.MySubclass) if \
callable(getattr(mymodule.MySubclass, func))]
## Remove doc for methods
for func in method_list:
## Change only original user defined methods
if ("_" not in el[0:2]):
original_method = getattr(mymodule.MySubclass, func)
setattr(original_method, "__doc__", "")
else:
pass
这可以很容易地封装在装饰器中,并在实例化子类或导入模块时调用。
发布于 2018-06-04 05:09:37
我能想到的唯一方法是,如果您在子类中创建基类中定义的所有函数,并在其中创建您自己的文档字符串,那么它们就可以被覆盖。您甚至可以留下空白文档字符串,无论您喜欢什么,但这将是非常不切实际的。
https://stackoverflow.com/questions/50670470
复制相似问题