Intermediate Code Comments #python #docstrings

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: not Returns:
  • 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.
"""