Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 5.9 — Docstrings and Type Hints: A First Look
Python · Example 1 of 2

Docstring, annotations and help()

Annotations are shown by help(), stored in __annotations__ and not enforced: the last call multiplies a string.

A docstring documents what a function does, and type hints (annotations) document what types its parameters and result should be. Python stores both on the function, shows them in help(), and lets tools read them, but it does not enforce the hints when the code runs.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Add a docstring and hints to def volume(length, width, height): return length * width * height. Print volume.__doc__ and volume.__annotations__.

02

Write describe_person(name: str, age: int = 0) -> str with a one-line docstring. Use inspect.signature to print its signature.

03

Show that a hint is not enforced: call a function annotated with int using a string and print the result.

Explain it without notes

01

What is a docstring and where does Python store it?

02

What do type hints do at run time?

03

Why write type hints if Python does not enforce them?

04

How can code read a function's hints, and why does that matter for AI tools?

Docstring, annotations and help()
Sign in to run this example in your browser.

Expected output

13.5
docstring: Return the area of a rectangle.

Both sides must use the same unit.

annotations: {'width': <class 'float'>, 'height': <class 'float'>, 'return': <class 'float'>}
ababab
Help on function area in module __main__:

area(width: float, height: float) -> float
    Return the area of a rectangle.

    Both sides must use the same unit.