python - 抑制子类文档字符串中的“基类继承的方法”
问题描述
我正在对具有大量方法和长文档字符串的类进行子类化。如果我调用 IPython 帮助函数,我会看到原始类的所有帮助。我想这是意料之中的,有没有办法抑制这个?我只想看到我重新定义的方法。
mymodule.py
是:
import matplotlib.axes
class MySubclass(matplotlib.axes.Axes):
pass
如果我在 IPython 中这样做:
import mymodule
help(mymodule)
打印输出很大,因为它包含所有“从 matplotlib.axes._axes.Axes 继承的方法:”这是兆字节的文本,因为它列出了类的所有方法的文档字符串。
解决方案
正如这里所讨论的,一个可能的工作解决方案是手动删除文档。
## 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
这可以很容易地封装在装饰器中,并在实例化子类或导入模块时调用。
推荐阅读
- reactjs - 你如何在 React 中截断 html 文本?
- selenium - 在 pytest selenium 中找不到日志类型“客户端”
- rabbitmq - RabbitMQ:在使用 DLX 策略发送到 DLQ 之前向消息头添加异常
- sql - 如何在另一列的基础上写一个where?
- javascript - Openlayers绘制交互:错误:单击后拖放地图
- php - 我的 SQL 需要 40 秒才能运行,但 PHP 会更快地执行相同的操作。这是为什么?
- asp.net - 安全审计问题 [For Asp.Net WebForms]:源代码公开
- angular - 角度项目的 npm 安装不起作用(下载 xmlbuilder 时 ECONNREFUSED)
- php - SQL - 获得准确的逗号分隔
- reactjs - CodeceptJS/Puppeteer 仅在暂停会话期间看到元素