Technical skill practice

Javadoc but for Python: Write and Inspect a Function Docstring

The closest built-in Python concept to Javadoc-style API documentation is a docstring: a string placed first in a module, class or function body. It can be inspected by help and documentation tools. Python does not require Java’s comment syntax or one universal third-party docstring format.

Start with a small, explicit task

Document a small function that labels a container count. State its accepted input and returned text, then inspect the attached documentation. Render its local text documentation with the standard library; the script does not build or claim to publish a documentation website.

Run the complete example

This original example was checked with Python 3.12.14. Save it as python_docstrings.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.

Expected output

Return a display label for a nonnegative integer count.

local documentation contains parameters and return description

import inspect
import pydoc
def container_label(count):
    """Return a display label for a nonnegative integer count.

    Args:
        count: Number of containers; must be nonnegative.

    Returns:
        Text such as '3 containers'.
    """
    if count < 0:
        raise ValueError("count must be nonnegative")
    return f"{count} containers"
doc = inspect.getdoc(container_label)
rendered = pydoc.render_doc(container_label, renderer=pydoc.plaintext)
assert container_label(3) == "3 containers"
assert doc.splitlines()[0].startswith("Return a display label")
assert "count" in rendered and "Returns:" in rendered
print(doc.splitlines()[0])
print("local documentation contains parameters and return description")

Explain the result

The triple-quoted first statement becomes the function’s attached documentation. inspect.getdoc retrieves a cleaned version, while pydoc renders a local text description. The assertions check that the expected parameter and return explanation are present without making the exercise depend on every formatting detail of the rendered help page.

The Args and Returns headings are a chosen readable convention, not keywords that make Python validate the interface. The function body separately checks its nonnegative rule. Comments inside the implementation could explain a decision, but ordinary # comments would not become this function’s docstring.

Avoid these mistakes

Keep documentation synchronized with actual behavior, especially exceptions and return values. This example assumes an integer count as stated; it does not claim that a docstring performs runtime type enforcement. Tools such as Sphinx add a separate publishing workflow, which has not been run by this exercise.

Questions, answered

Does Python use /** ... */ for docstrings?

No. This example uses a string as the first function statement.

Did this generate a documentation website?

No. It checks introspection and a local standard-library text rendering only.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.