0Pricing
Python Academy · Lesson

The attrs Library

Compare attrs with dataclasses.

The attrs Library is a free Python Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)

Slots by Default

A big difference: @define classes use __slots__ automatically. This lowers memory use and prevents accidentally adding undeclared attributes. Plain dataclasses use a regular __dict__ unless you pass slots=True (Python 3.10+).

Built-in Validators

attrs ships ready-made validators. A field can declare a validator that runs during construction and raises if the value is invalid. Dataclasses have no built-in validation; you write it yourself in __post_init__.

from attrs import define, field
from attrs.validators import instance_of

@define
class User:
    age: int = field(validator=instance_of(int))

User(30)        # ok
User('oops')    # raises TypeError

Converters

A converter transforms the input value before it is stored. For example, coerce a string to an int automatically. This is another feature dataclasses lack.

from attrs import define, field

@define
class Item:
    price: int = field(converter=int)

i = Item('42')   # stored as the integer 42
print(i.price)

The Dataclass Equivalent

Here is the same idea expressed with the standard library. Validation and conversion go into __post_init__ by hand, since dataclasses do not offer hooks per field.

from dataclasses import dataclass

@dataclass
class Item:
    price: int

    def __post_init__(self):
        self.price = int(self.price)

print(Item('42'))

asdict in Both

Both libraries provide an asdict helper. The dataclass one lives in dataclasses; the attrs one lives in attrs. The behavior is nearly identical.

from dataclasses import dataclass, asdict

@dataclass
class Point:
    x: int
    y: int

print(asdict(Point(1, 2)))

Frozen in attrs

attrs supports immutability too. Use @frozen (a shortcut for @define(frozen=True)) for read-only instances, mirroring @dataclass(frozen=True).

from attrs import frozen

@frozen
class Point:
    x: int
    y: int

p = Point(1, 2)
print(p)
# p.x = 9  would raise FrozenInstanceError

Feature Comparison

How they line up:

  • dataclasses: standard library, zero install, covers basics.
  • attrs: validators, converters, slots by default, more configuration.
  • Both generate __init__, __repr__, __eq__.

Which Should You Use?

Choose dataclasses for simple data holders with no extra dependency. Choose attrs when you need validators, converters, or its richer configuration, and you are comfortable adding a dependency. Many projects start with dataclasses and migrate only if they outgrow them.

A Shared Heritage

Because dataclasses borrowed so much from attrs, migrating between them is usually mechanical: swap the import and decorator, and adjust validators or converters. Knowing one makes the other immediately familiar.

  • @define maps to @dataclass.
  • attrs.field maps to dataclasses.field.
  • Validators move into __post_init__ when going to dataclasses.

Quick Check

Which capability does attrs offer that the standard dataclasses module does not provide built in?

Recap

You compared attrs with dataclasses.

  • attrs inspired dataclasses and offers more features.
  • attrs adds validators, converters, and slots by default.
  • dataclasses is built in and needs no install.
  • Pick the simplest tool that meets your needs.

Frequently asked questions

Is the “The attrs Library” lesson free?

Yes — the full text of “The attrs Library” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “The attrs Library”?

Compare attrs with dataclasses. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The attrs Library” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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