October 2018
Intermediate to advanced
420 pages
10h 26m
English
Before Python 3.7, namedtuple was the easiest way to use class-like objects without having to implement a class for each object with the associated constructor. Since Python 3.7, dataclasses can also be used for this purpose. dataclass allows a class to easily declare that it contains only data. This is available in the dataclasses module of the standard library. The same snippet can be written as follows with dataclasses:
from dataclasses import dataclass@dataclass class Basket: count what@dataclass class FruitCount: count doubleobs = Observable.from_([Basket(count=5, what='apple'), Basket(count=3, what='orange')]) \ .filter(lambda i: i.what == 'apple') \ .map(lambda i: i.count) \ .map(lamda i: FruitCount(count=i, double=i*2)) ...