Type hints
When you use a function from a module, you usually don't read its code. You read its first line, and that line should tell you what to pass and what you get back. That's what the type hints you met in part 4 are for, and with modules they earn their keep:
def with_tax(price: float, percent: int) -> float:
return price * (1 + percent / 100)
price: floatsays the parameterpriceshould be a float, andpercent: intan integer.-> float, between the parentheses and the colon, says what the function returns. A function that returns nothing has-> None.- A hint can also say what a list or a dictionary holds:
list[int]is a list of integers,dict[str, int]a dictionary with string keys and integer values, andtuple[int, int]a tuple of two integers.
Here is a module with hints, and a program that uses it:
import scores
points = [7, 9, 4, 10]
print(scores.best(points))
print(scores.passed(points, 5))def best(points: list[int]) -> int:
return max(points)
def passed(points: list[int], limit: int) -> int:
count = 0
for p in points:
if p >= limit:
count += 1
return count10
3Reading only the first lines of scores.py, you know that both functions want a list of integers and return an integer, and that passed also wants a limit. Editors such as Visual Studio Code show these first lines when you type a module's name and a dot, so you don't even need to open the module's file. A module is written once and used by many programs, often by other people, so these few words in its first lines save a lot of reading.
When the hint is ignored
Python itself doesn't check the hints, as you saw in part 4. A call with the wrong type runs, and goes wrong only where the value is used. With modules, that place is in another file, and the traceback has a line for each file on the way:
import money
print(money.with_tax("10", 24))def with_tax(price: float, percent: int) -> float:
return price * (1 + percent / 100)Traceback (most recent call last):
File "main.py", line 3, in <module>
print(money.with_tax("10", 24))
~~~~~~~~~~~~~~^^^^^^^^^^
File "money.py", line 2, in with_tax
return price * (1 + percent / 100)
~~~~~~^~~~~~~~~~~~~~~~~~~~~
TypeError: can't multiply sequence by non-int of type 'float'Read it from the bottom: the error happened in money.py, on line 2, where a string met a multiplication. The line above names the call in main.py that got there. The hint price: float told the caller what to pass; the caller passed the string "10". Separate checker programs (such as mypy) can find mistakes like this from the hints before the program runs, but Python doesn't.