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

Suppress "Methods inherited by base class" in subclass docstring

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

mymodule.py 是:

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

如果我在 IPython:

import mymodule
help(mymodule)

打印输出很大,因为它包含所有 "Methods inherited from matplotlib.axes._axes.Axes:",这是数兆字节的文本,因为它列出了 class.

的所有方法的文档字符串

我能想到的唯一方法是,如果您在 child class 中创建基础 class 中定义的所有函数,并在其中创建自己的文档字符串,那么它们可以被覆盖。你甚至可以保留空白的文档字符串,无论你喜欢什么,但这将是非常不切实际的。

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

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