When using mypy, we'll need to provide type hints for the attributes of a class. This is generally handled through the __init__() method. Most of the time, the parameter type hints are all that's required.
In previous examples, we defined classes like this:
class RTD_Solver:
def __init__(
self, *,
rate: Optional[float] = None,
time: Optional[float] = None,
distance: Optional[float] = None
) -> None:
if rate:
self.rate = rate
if time:
self.time = time
if distance:
self.distance = distance
The type hints on the parameters are used to discern the types for the instance variables, self.rate, self.time, and self.distance.
When we assign default values in the __init__() method, we have two common design patterns.
- When we can compute a...