December 2017
Beginner
412 pages
10h 8m
English
When writing type contracts for functions, often we’ll want to specify that the values in a list parameter are all of a particular type. For example, we might write a function to calculate the average of a list of floats:
| | >>> def average(L: list) -> float: |
| | ... """Return the average of the values in L. |
| | ... |
| | ... >>> average([1.4, 1.6, 1.8, 2.0]) |
| | ... 1.7 |
| | ... """ |
There is currently no indication that the function works only with lists of numbers, but it would be odd to call it with a list of strings, for example. To address this, Python includes module typing that allows us to specify the expected type of value contained in a list (and in other types that you’ll encounter in Chapter ...
Read now
Unlock full access