Python Glossary Русский

all Python 3inspectfunction

inspect.cleandoc()

source code

Strips from a docstring the indentation it had inside the code, plus the blank lines at its start and end.

Syntax

inspect.cleandoc(doc)

A docstring's first line usually follows the quotes with no indent, while the rest carry the function's indentation. textwrap.dedent looks for a margin common to all lines and removes nothing from such text; cleandoc treats the first line separately and removes the common margin from the second line on, keeping deeper indentation. Tabs are expanded to spaces. Since 3.13 the compiler itself strips the common margin from __doc__, but text from other sources and older versions still needs cleandoc; getdoc() calls it for you.

Examples

import inspect, textwrap
doc = 'Первая строка.\n\n        Вложенный отступ.\n    Конец.\n    '
print(repr(textwrap.dedent(doc).splitlines()[3]))  # → '    Конец.' — отступ остался
print(repr(inspect.cleandoc(doc)))
# → 'Первая строка.\n\n    Вложенный отступ.\nКонец.'

See also

Open in the full glossarydocs.python.orgReport an inaccuracy