python - 抑制子类文档字符串中的 "Methods inherited by base class"

标签 python inheritance ipython docstring

我正在对一个具有大量方法和长文档字符串的类进行子类化。如果我调用 IPython 帮助函数,我会看到原始类的所有帮助。 我想这是预料之中的,有没有办法抑制它?我只想查看我重新定义的方法。

mymodule.py 是:

import matplotlib.axes
class MySubclass(matplotlib.axes.Axes):
    pass

如果我在 IPython 中这样做:

import mymodule
help(mymodule)

打印输出很大,因为它包含所有“从 matplotlib.axes._axes.Axes 继承的方法:”,这是兆字节的文本,因为它列出了该类的所有方法的文档字符串。

最佳答案

正如所讨论的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

这可以很容易地封装在装饰器中,并在实例化子类或导入模块时调用。

关于python - 抑制子类文档字符串中的 "Methods inherited by base class",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50670470/

相关文章:

python - 让 .eq 忽略 NAN 值

python - 如何使用 Python 在 Selenium 中测试 Alert/Popup

python - 为什么继续不起作用?

c++ - VS2013中继承构造函数

c++ - 如何从此类实例的指针使用类的方法?

c++ - 防止继承 operator new 和 delete

python - 如何使用 IPython 显示连续显示图像?

python - 运行 df.to_csv() 时出现 Dask 内存错误

Python:如何获取仅出现在集合列表中的一组中的项目?

linux - libgfortran.so.1 : cannot open shared object file