Dataclasses
@dataclass writes __init__, __repr__ and __eq__ for you. Use field defaults and frozen=True for immutability.
Example
from dataclasses import dataclass
@dataclass(frozen=True)
class P:
x: int
y: int = 0
print(P(1), P(1) == P(1, 0))Result
P(x=1, y=0) True
Exercise
Dataclass decorator.
@ class A: pass
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.