OBJECTSChapter 8 · Objects and Classes
Type hints
Type hints label what a variable or function expects: def area(w: float, h: float) -> float:. Python does not enforce them while running, but editors and checkers such as mypy read them to catch mistakes before you run, and they document your code for the next reader. list[str], dict[str, int] and str | None describe containers and optional values.
Worked example
How it reads
name: typeafter each parameter,-> typefor the return valueint | Nonemeans the function might return nothing- Hints are notes for tools and people; the code runs the same without them

Cloud tip: Type hints pay off most on functions other people call. Start there.


