Type hints: labels on your values
🌍 In real life. In a kitchen, the jars have labels: sugar, salt, rice. A label tells everyone what should be inside, so nobody puts salt in their tea. But a label is only a sticker: nothing stops you pouring salt into the sugar jar.
A type hint is that label for a Python value. Step by step:
- after a name,
: intsays what kind of value it should hold:age: int = 15 - in a function, each parameter can have one:
def double(n: int) -> intafter the brackets says what the function gives back- Python saves the labels (in
__annotations__), but it doesn't check them when the code runs
The idea in one sentence: a type hint like age: int or -> str is a label that says what a value should be; Python doesn't check it when the program runs, but people and tools read it.
Its words:
- type hint (annotation):
name: str,def area(w: int) -> int: ->: the hint for what a function returns- type checker: a tool (like mypy) that reads the hints and warns before the code runs
age: int = 15
name: str = "Mina"
happy: bool = True
def double(n: int) -> int:
return n * 2
print(name, age, happy)
print(double(4))Watch the label inspector. Each value arrives as a card coloured by its real type (blue int, green str). A scanner over each slot compares the card with the label. Notice what happens when the label says int but a str arrives: the light turns amber, and the card goes in anyway.
So why write them? Because people and tools read them, before the code ever runs:
- you, next month:
def area(w: int, h: int) -> inttells you how to call it without reading the body - your editor (like VS Code) uses them to suggest the right methods and to underline mistakes as you type
- a type checker (a separate program such as mypy or pyright) reads every hint and warns: "line 9:
areaexpects an int, got a str"
This app runs your code but has no type checker, so here the hints are for you. Python keeps them in a dictionary called __annotations__:
def area(w: int, h: int) -> int:
return w * h
print(area(3, 4))
print(area("ab", 3))
print(area.__annotations__)Mistakes and edge cases.
- Expecting Python to stop a wrong type.
def f(n: int) -> int: return n * 2called with"ab"happily returns"abab": the label isn't a lock. - Hints that lie. A hint that no longer matches the code misleads every reader; update it with the code.
- Overdoing it. Hint function parameters and returns; a plain
count = 0inside a function doesn't need: int.
Quiz
What does this print?
def half(n: int) -> int: return n // 2 print(half(9))def double(n: int) -> intis called asdouble("ab"). What does Python do?- Runs it anyway and gives back 'abab'
- Raises a TypeError because n should be an int
- Turns "ab" into an int first
What does this print?
def twice(s: str) -> str: return s * 2 print(twice(3))What does
-> boolafter a function's brackets mean?- The function should give back a bool
- Every parameter must be a bool
- Python turns the answer into a bool
Which line has a mistake?
def greet(name: str) => str: return "Hi " + name print(greet("Mina"))
Coding tasks
Add type hints to
full_name: both parameters arestr, and it gives back astr. Keep what it does the same.Finish
is_adult(age: int) -> bool: it gives backTruewhenageis 18 or more, otherwiseFalse.