Functions
Type hints and docstrings
Describe function inputs and outputs for readers without changing runtime validation.
Lesson 27 of 63
Reading: about 2 min
Lesson + practice: about 12 min
Difficulty: Beginner
On this page4 sections
Type hints describe the kinds of values a function expects and returns. A docstring, written immediately inside the function, explains its purpose. Python does not enforce ordinary type hints when the function runs.
def total_minutes(sessions: int, minutes_each: int) -> int:
"""Return the total planned study minutes."""
return sessions * minutes_each
print(total_minutes(3, 25))
print(total_minutes.__doc__)75 Return the total planned study minutes.
Hints are guidance
A hint does not convert a string response from input() into an integer or reject a wrong argument. Convert and validate values separately. Hints help editors and checking tools spot mistakes.
Why this works
The colon hints describe parameters, and the arrow hint describes the return value. A docstring is stored on the function and can be read through __doc__ or help(). These are tools for readers and static checkers; they do not replace input conversion or runtime validation.
Practice
Add type hints and a one-sentence docstring to your hours-and-minutes function. Call it with integers and check the result.
Will -> int make Python reject every non-integer result at runtime?
Reveal answer
No. Standard type hints are not enforced automatically at runtime.
Try another case
Does def double(x: int) -> int automatically reject the string "4" before the body runs?
Check your answer
Reveal answer
No. Standard Python type hints are not automatically enforced at runtime.
Your progress
Reveal the check answer or reach the end to complete this lesson.