Home

attrs

attrs is a Python library that provides a lightweight, declarative way to define classes with attributes. It focuses on reducing boilerplate by automatically generating common methods and by offering attribute-level features such as validation, conversion, and metadata. The project predates Python’s standard library dataclasses and remains popular for its rich customization options.

A central concept is the attr.s decorator used to define a class and attr.ib for individual attributes.

Validators and converters provide a flexible mechanism for data integrity. Built-in validators cover common checks (e.g.,

Additional utilities include asdict and astuple for deep serialization, and evolve for creating modified copies of

Patterns and ecosystem: attrs is widely used in Python projects as an alternative to dataclasses when advanced

Attributes
can
have
default
values,
default
factories,
converters
that
transform
input,
and
validators
that
enforce
constraints.
The
auto_attribs
feature
allows
defining
attributes
with
Python
type
hints,
while
slot
and
frozen
options
enable
memory
efficiency
and
immutability.
Generated
methods
typically
include
__init__,
__repr__,
and
__eq__,
with
optional
ordering
and
custom
hashing
behavior.
instance_of,
in_,
matches).
Converters
transform
incoming
values
(for
example,
parsing
strings
to
numbers)
before
storage.
Attrs
also
supports
metadata
and
metadata-driven
tooling,
enabling
integration
with
other
libraries
and
tooling.
existing
instances.
The
library
emphasizes
readability
and
explicitness,
making
class
definitions
clearer
and
easier
to
maintain.
attribute
handling
is
needed.
It
remains
compatible
with
multiple
Python
versions
and
can
interoperate
with
typing
and
other
data-validation
libraries.