June 2019
Beginner to intermediate
770 pages
19h 24m
English
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 dataclassfrom typing import Optional, cast@dataclassclass 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 ...
Read now
Unlock full access