Starting with Python 3.7 the dataclasses module is available. This module offers a superclass we can use to create classes with clearly-stated attribute definitions. The core use case for a dataclass is a simple definition of the attributes of a class.
The attributes are used to automatically create common attribute access methods, including __init__(), __repr__(), and __eq__(). Here's an example:
from dataclasses import dataclass
from typing import Optional, cast
@dataclass
class RTD:
rate: Optional[float]
time: Optional[float]
distance: Optional[float]
def compute(self) -> "RTD":
if (
self.distance is None and self.rate is not None
and self.time is not None
):
self.distance = self.rate * self.time
elif (
self.rate is None and self.distance is not None
...