Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Python
  3. attrs

attrs

MITPython26.1.0

A Python package that eliminates boilerplate code when writing classes by automatically implementing object protocols.

Visit WebsiteGitHubGitHub
5.8k stars428 forks0 contributors

What is attrs?

attrs is a Python library that eliminates the need to write repetitive boilerplate code when defining classes. It automatically generates common object protocols (dunder methods) like `__init__`, `__repr__`, and `__eq__` based on declaratively defined attributes, allowing developers to focus on business logic. It solves the problem of maintaining verbose and error-prone class definitions while ensuring runtime performance.

Target Audience

Python developers who write many data-oriented or complex classes and want to reduce boilerplate, improve code clarity, and ensure correctness without performance overhead.

Value Proposition

Developers choose attrs over alternatives because it is more flexible and feature-rich than Python's built-in `dataclasses`, offers backward-compatible APIs, and is trusted in high-stakes environments like NASA missions. It provides extensive customization hooks and works seamlessly with or without type annotations.

Overview

Python Classes Without Boilerplate

Use Cases

Best For

  • Reducing boilerplate in data-heavy Python classes
  • Automatically generating `__repr__` and `__eq__` methods
  • Creating classes that require custom initialization logic
  • Ensuring class correctness without manual dunder method implementation
  • Migrating from older class patterns to modern declarative styles
  • Handling complex equality checks (e.g., with NumPy arrays)

Not Ideal For

  • Projects that strictly avoid external dependencies and rely solely on Python's standard library
  • Classes requiring dynamic attribute addition or removal after instantiation
  • Simple scripts or one-off classes where learning a new API adds unnecessary overhead
  • Teams that prefer the built-in simplicity of dataclasses for basic data containers

Pros & Cons

Pros

Declarative Simplicity

Defines attributes with type hints or field() for a clear, concise class overview, eliminating repetitive boilerplate as shown in the @define example.

Automatic Method Generation

Generates common dunder methods like __init__, __repr__, and __eq__ automatically based on declared attributes, ensuring correctness and reducing manual errors.

Performance Parity

Ensures no runtime performance penalties compared to manually written classes, a key feature highlighted in the README as a core goal.

Flexible Customization

Offers hooks for custom initialization and special handling, such as NumPy array equality checks, providing more control than basic alternatives.

Cons

API Complexity

Supports both modern (@define) and classic (@attr.s) APIs indefinitely, which can confuse users and increase the learning curve compared to simpler tools.

External Dependency

Requires installing a third-party package, adding an extra dependency that may not be acceptable in minimalist or strictly controlled environments.

Over-Engineering Risk

For very simple classes with few attributes, the declarative syntax can introduce unnecessary abstraction and complexity over plain Python classes.

Frequently Asked Questions

Quick Stats

Stars5,774
Forks428
Contributors0
Open Issues132
Last commit7 days ago
CreatedSince 2015

Tags

#boilerplate-reduction#oop#boilerplate#developer-productivity#python#type-annotations#attributes

Built With

P
Python

Links & Resources

Website

Included in

Python290.8k
Auto-fetched 1 day ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub