The attrs Library
Compare attrs with dataclasses.
What Is attrs?
attrs is a third-party library that pioneered the class-from-fields idea years before dataclasses entered the standard library. The standard dataclasses module was directly inspired by it.
- attrs is a separate install:
pip install attrs. - It offers more features than dataclasses.
- dataclasses is built in and covers most everyday needs.
The Modern attrs API
Modern attrs uses @define and field() from the attrs namespace. The shape is very similar to a dataclass.
from attrs import define, field
@define
class Point:
x: int
y: int
p = Point(1, 2)
print(p)