Skip to content
Back to top

# attrs: Classes Without Boilerplate

Release 25.4.0 (What’s new?)

attrs is the Python package that will bring back the joy of writing classes by relieving you from the drudgery of implementing object protocols (aka dunder methods). Trusted by NASA for Mars missions since 2020!

Its main goal is to help you to write concise and correct software without slowing down your code.

## Sponsors

attrs would not be possible without our amazing sponsors. Especially those generously supporting us at the The Organization tier and higher:

Please consider joining them to help make attrs’s maintenance more sustainable!

## Getting Started

attrs is a Python-only package hosted on PyPI.

The following steps will get you up and running in no time:

- Overview will show you a simple example of attrs in action and introduce you to its philosophy. Afterwards, you can start writing your own classes and understand what drives attrs’s design.

- attrs by Example will give you a comprehensive tour of attrs’s features. After reading, you will know about our advanced features and how to use them.

- Why not… gives you a rundown of potential alternatives and why we think attrs is still worthwhile – depending on your needs even superior.

- If at any point you get confused by some terminology, please check out our Glossary.

If you need any help while getting started, feel free to use the `python-attrs` tag on Stack Overflow and someone will surely help you out!

## Day-to-Day Usage

- Type Annotations help you to write correct and self-documenting code. attrs has first class support for them, yet keeps them optional if you’re not convinced!

- Instance initialization is one of attrs key feature areas. Our goal is to relieve you from writing as much code as possible. Initialization gives you an overview what attrs has to offer and explains some related philosophies we believe in.

- Comparing and ordering objects is a common task. Comparison shows you how attrs helps you with that and how you can customize it.

- If you want to put objects into sets or use them as keys in dictionaries, they have to be hashable. The simplest way to do that is to use frozen classes, but the topic is more complex than it seems and Hashing will give you a primer on what to look out for.

- Once you’re comfortable with the concepts, our API Reference contains all information you need to use attrs to its fullest.

- attrs is built for extension from the ground up. Extending will show you the affordances it offers and how to make it a building block of your own projects.

- Finally, if you’re confused by all the `attr.s`, `attr.ib`, `attrs`, `attrib`, `define`, `frozen`, and `field`, head over to On The Core API Names for a very short explanation, and optionally a quick history lesson.

## attrs for Enterprise

Available as part of the Tidelift Subscription.

The maintainers of attrs and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source packages you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use.

## Full Table of Contents

Getting Started

- Overview
  - Philosophy
  - What attrs Is Not
- Why not…
  - … Data Classes?
  - … Pydantic?
  - … namedtuples?
  - … tuples?
  - … dicts?
  - … hand-written classes?
- attrs by Example
  - Basics
  - Converting to Collections Types
  - Defaults
  - Validators
  - Conversion
  - Metadata
  - Types
  - Slots
  - Immutability
  - Other Goodies

Explanations

- Type Annotations
  - Mypy
  - Pyright
  - Class variables and constants
- Initialization
  - Private Attributes and Aliases
  - Defaults
  - Validators
  - Converters
  - Hooking Yourself Into Initialization
  - Order of Execution
  - Derived Attributes
  - attrs and `__init_subclass__`
- Comparison
  - Customization
- Hashing
  - Hash Method Generation
  - Hashing and Mutability
  - Hash Code Caching

Reference

- API Reference
  - Core
  - Exceptions
  - Helpers
  - Validators
  - Converters
  - Setters
- API Reference for the `attr` Namespace
  - Core
  - Exceptions
  - Helpers
  - Validators
  - Converters
  - Setters
  - Deprecated APIs
- Glossary

Advanced

- Extending
  - Wrapping the Decorator
  - Types
  - Metadata
  - Automatic Field Transformation and Modification
  - Customize Value Serialization in `asdict()`
- How Does It Work?
  - Boilerplate
  - Immutability
  - Cached Properties on Slotted Classes

Meta

- On The Core API Names
- License and Credits
- Changelog
- PyPI
- GitHub
- Third-party Extensions
- Contributing
- Funding

Full Index