Getting the docstring from a function
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Every Python function can have a docstring — a string literal as the first statement in the function body. Python stores this string in the function's __doc__ attribute, making it accessible at runtime. You can retrieve a function's docstring with function.__doc__, format it with inspect.getdoc(), or use help() for interactive viewing. Docstrings power Python's built-in help system, IDE tooltips, and documentation generators like Sphinx.
Accessing with doc
Output (preserves original whitespace):
__doc__ returns the raw string with original indentation intact.
Cleaned Docstring with inspect.getdoc()
Output (indentation cleaned up):
inspect.getdoc() strips leading whitespace from all lines and trims blank lines at the start and end. This gives you the docstring as it was intended to be read.
Using help()
Output:
help() shows the function signature alongside the docstring. It is designed for interactive exploration in the REPL.
Docstrings on Methods and Classes
Docstrings on Modules
A module's docstring is the first string literal in the file:
Functions Without Docstrings
If a function has no docstring, __doc__ is None. Always check for None before processing.
Docstring Styles
Python has three common docstring conventions:
All three are stored the same way in __doc__ — the style only matters for documentation generators.
Programmatic Docstring Inspection
This pattern is useful for building custom help systems, API documentation, or command-line tool --help output.
Decorators and Docstrings
Decorators can accidentally overwrite a function's docstring:
Fix with functools.wraps:
Always use @functools.wraps in decorators to preserve the wrapped function's metadata.
Common Pitfalls
- Confusing comments with docstrings: A
# commentabove or inside a function is not a docstring. The docstring must be a string literal as the first statement in the body. - Forgetting
functools.wrapsin decorators: Decorators replace the function object. Without@wraps,__doc__,__name__, and other attributes are lost. - Raw
__doc__has extra whitespace: Useinspect.getdoc()for cleaned output. Raw__doc__preserves the indentation from the source code. - Docstring is
None: Functions without a docstring literal have__doc__ = None. Check forNonebefore calling.split()or other string methods on it. help()in scripts vs REPL:help()uses a pager in interactive mode but prints directly when piped or in a script. For programmatic use, access__doc__directly.
Summary
- Access a function's docstring with
func.__doc__(raw) orinspect.getdoc(func)(cleaned) - Use
help(func)for interactive viewing with signature and formatting - Docstrings work on functions, methods, classes, and modules
__doc__isNoneif no docstring is defined- Use
@functools.wrapsin decorators to preserve the wrapped function's docstring - Choose a consistent style (Google, NumPy, or Sphinx) for documentation generators
Related reading
- Getting the exception value in Python
- getting the index of a row in a pandas apply function
- Getting the index of the returned max or min item using max/min on a list
- Getting the name of a variable as a string
- Getting the SQL from a Django QuerySet
- Getting today's date in YYYY-MM-DD in Python?
- Getting today's date in YYYY-MM-DD in Python?
- Getting TypeError ''sliceNone, None, None, 0'' is an invalid key
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.