all Python 3inspectfunction
inspect.getdoc()
source code
An object's docstring with indentation cleaned up; if it has none, it is taken from the parent class. Returns None when there is no docstring at all.
Syntax
inspect.getdoc(object)
It differs from __doc__ in two ways. First, the text goes through cleandoc, so the indentation the docstring had inside the function is removed. Second, an overriding method without a docstring gets the parent's (since 3.5), and a class without one gets its base's, where obj.__doc__ is None. That is what help() shows; read __doc__ when you need the object's own string only.
Examples
import inspect
class Base:
def run(self):
"""Запустить задачу."""
class Child(Base):
def run(self):
pass
print(Child.run.__doc__) # → None
print(inspect.getdoc(Child.run)) # → Запустить задачу.
import inspect
def f():
"""Заголовок.
Подробности.
"""
print(repr(inspect.getdoc(f))) # → 'Заголовок.\n\nПодробности.'
See also
Open in the full glossarydocs.python.orgReport an inaccuracy