0Pricing
Python Academy · Lesson

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)

All lessons in this course

  1. Defining a Dataclass
  2. Default Values and field()
  3. Frozen and Comparison Options
  4. The attrs Library
← Back to Python Academy