Search code examples
pythoninheritanceipythondocstring

Suppress "Methods inherited by base class" in subclass docstring


I am subclassing a class with a large number of methods and long docstrings. If I call the IPython help function I see all the help of the original class. I guess this is expected, is there a way to suppress this? I'd like to see only the methods I redefined.

mymodule.py is:

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

If I do in IPython:

import mymodule
help(mymodule)

The printout is huge because it contains all of the "Methods inherited from matplotlib.axes._axes.Axes:" which is megabytes of text because it lists the docstrings of all the methods of the class.


Solution

  • As discussed here, a possible working solution would be to remove the doc manually

    ## 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
    

    This could be easily encapsulated in a decorator, and called when instantiating the subclass or importing the module.