type hint
A note in a function heading or variable declaration saying what type a value should be, such as def total(prices: list[float]) -> float:.
In more detail
Python ignores hints while the program runs: a wrong type still runs, or fails exactly as it would without them. Tools such as mypy and editors read them to catch mistakes early and to offer better completions. They pay off most on functions other people call.
Example
def greet(name: str) -> str:
return "Hello, " + nameRelated terms
- mypy — A static type checker for Python that reads type hints and reports code whose types do not fit together, without running it.
- union type — A type that allows one of several listed types or values, written with a vertical bar, such as int | None in Python or "small" | "large" in TypeScript.
- dynamic typing — A language design where a variable can hold a value of any type and types are only checked while the program runs.
- static type checking — Checking that values are used consistently with their declared types by analysing code without running it.
Taught in
- Floats, Division And Mixed Types — Hands-on Python: Complete Video Course & Book
- A Standard Library Tour — Hands-on Python: Complete Video Course & Book
- Testing With pytest — Hands-on Python: Complete Video Course & Book
- Type Hints — Hands-on Python: Complete Video Course & Book