Docstrings and doctests

A string literal as the first line of a function body is its docstring. It is stored in func.__doc__, and help(func) shows it. The usual shape is a one-line summary, a blank line, then details.

A doctest is an example inside the docstring, written like an interactive session: a >>> line with a call, then the output you expect. The doctest module finds these, runs them, and reports any that print something else.

python3 -m doctest main.py -v

Doctests keep examples honest: if the code changes, a stale example fails. They are not a replacement for real tests, but in an interview, writing two or three example calls before the body is a great habit. It pins down the input shape and the edge cases with the interviewer.

The program runs its own doctests with doctest.testmod(), which prints nothing for passing examples and a report for each failing one.

Run it, then change 'AL' to 'A.L.' and run again to see a failure report.