Python Docstrings
6 exercises — identify and write Google-style, NumPy-style, and Sphinx-style Python docstrings in real engineering contexts.
0 / 6 completed
Three Python docstring styles
- Google:
Args:/Returns:/Raises:— popular in general Python projects - NumPy:
Parameters\n----------— standard in numpy/pandas/scikit-learn - Sphinx:
:param name:/:type name:/:returns:— native Sphinx format - PEP 257: First line = imperative mood, one sentence, ends with period
- Generators: use
Yields:notReturns: - Rule: match the existing codebase — consistency beats style preference
1 / 6
What style is this Python docstring?
def parse_date(date_str):
"""Parse a date string into a datetime object.
Args:
date_str (str): Date string in ISO 8601 format (YYYY-MM-DD).
Returns:
datetime: Parsed datetime object.
Raises:
ValueError: If the string does not match ISO 8601 format.
"""
Google style docstring — recognisable by its indented sections with a colon-terminated header.
Google style sections:
• Args: — list of parameters (name (type): description)
• Returns: — return type and description
• Raises: — exceptions with conditions
• Note:, Example:, Yields: — optional sections
When to use Google style:
• Most popular in general Python projects
• Used by Google, many open-source projects
• Works well with Sphinx via the
• Preferred by Black, isort, and most Python style guides
Linted/generated by: Google's own styleguide, pylint, pydocstyle, mkdocs.
Google style sections:
• Args: — list of parameters (name (type): description)
• Returns: — return type and description
• Raises: — exceptions with conditions
• Note:, Example:, Yields: — optional sections
When to use Google style:
• Most popular in general Python projects
• Used by Google, many open-source projects
• Works well with Sphinx via the
sphinx.ext.napoleon extension• Preferred by Black, isort, and most Python style guides
Linted/generated by: Google's own styleguide, pylint, pydocstyle, mkdocs.