whatisgithub

What is attrs?

python-attrs/attrs — explained in plain English

Analysis updated 2026-06-26

5,780PythonAudience · developerComplexity · 2/5Setup · easy

In one sentence

attrs is a Python library that auto-generates class boilerplate, __init__, __repr__, and comparison methods, from a simple @define decorator, so you stop writing repetitive code every time you define a class.

Mindmap

mindmap
  root((repo))
    What it does
      Generates __init__
      Generates __repr__
      Equality methods
      Reduces boilerplate
    Tech Stack
      Python
      PyPI package
    Use Cases
      Data models
      JSON serialization
      NumPy equality
    Key Features
      Validators
      Converters
      asdict function
      Type annotations
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

What do people build with it?

USE CASE 1

Auto-generate __init__, __repr__, and comparison methods for Python classes without writing repetitive boilerplate code.

USE CASE 2

Build data models with validators and converters that automatically validate and transform values on creation.

USE CASE 3

Convert attrs class instances to plain dictionaries using asdict() for JSON output or structured logging.

USE CASE 4

Use attrs as a more flexible alternative to Python dataclasses when you need finer control over initialization hooks or custom equality logic.

What is it built with?

Python

How does it compare?

python-attrs/attrsiamtomshaw/f1-race-replayaidlearning/aidlearning-framework
Stars5,7805,7795,773
LanguagePythonPythonPython
Setup difficultyeasyeasymoderate
Complexity2/52/53/5
Audiencedevelopergeneraldeveloper

Figures from each repo's GitHub metadata at analysis time.

How do you get it running?

Difficulty · easy Time to first run · 5min
The explanation does not mention the license terms.

So what is it?

attrs is a Python library that reduces the amount of repetitive code needed when writing classes. In standard Python, defining a class with several attributes means writing the same patterns over and over: an __init__ method to accept and assign each value, a __repr__ method so the object prints usefully, equality methods so two instances can be compared, and so on. attrs generates all of that automatically from a simple declaration. You define a class with the @define decorator, then list your attributes as annotated fields. From that declaration, attrs creates a working initializer with default values, a readable string representation, and comparison operators, all without you writing any of that code manually. Type annotations are supported but not required. If you prefer not to annotate types, you can use attrs.field() assignments instead. The library predates Python's built-in dataclasses module, which was directly influenced by attrs. The two cover similar ground, but attrs goes further in several areas: more control over initialization hooks, better support for custom equality logic (useful when working with libraries like NumPy), and a debugger-friendly approach to generated methods. The README points to a comparison page for a full breakdown of the differences. attrs works well with validators, converters, and custom serialization. The asdict() function converts an attrs instance to a plain dictionary, which is useful for JSON output or logging. The library has a community of third-party extensions listed in the project wiki, covering things like serialization formats and integrations with other frameworks. The project is well-established and has been used in production environments including NASA Mars missions. It is installable from PyPI with a standard pip install. Documentation is available at attrs.org, and support questions can be asked on Stack Overflow with the python-attrs tag. The library has been in active development since 2015.

Copy-paste prompts

Prompt 1
Using the attrs library, create a Python class for a Product with name, price, and quantity fields, including a validator that raises an error if price is negative.
Prompt 2
Rewrite this Python class using attrs @define so I don't need to write __init__ or __repr__ manually: [paste your class here]
Prompt 3
Show me how to use attrs converters to automatically strip whitespace from string fields when constructing an instance.
Prompt 4
How do I use attrs asdict() to convert a nested attrs class structure into a JSON-serializable dictionary?
Prompt 5
I'm using NumPy arrays as fields in an attrs class. Show me how to set up custom equality logic so two instances compare element-wise correctly.

Frequently asked questions

What is attrs?

attrs is a Python library that auto-generates class boilerplate, __init__, __repr__, and comparison methods, from a simple @define decorator, so you stop writing repetitive code every time you define a class.

What language is attrs written in?

Mainly Python. The stack also includes Python.

What license does attrs use?

The explanation does not mention the license terms.

How hard is attrs to set up?

Setup difficulty is rated easy, with roughly 5min to a first successful run.

Who is attrs for?

Mainly developer.

Open on GitHub → Ask about another repo

This repo across BitVibe Labs

Verify against the repo before relying on details.