A Python package that eliminates boilerplate code when writing classes by automatically implementing object protocols.
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.
Python developers who write many data-oriented or complex classes and want to reduce boilerplate, improve code clarity, and ensure correctness without performance overhead.
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.
Python Classes Without Boilerplate
Defines attributes with type hints or field() for a clear, concise class overview, eliminating repetitive boilerplate as shown in the @define example.
Generates common dunder methods like __init__, __repr__, and __eq__ automatically based on declared attributes, ensuring correctness and reducing manual errors.
Ensures no runtime performance penalties compared to manually written classes, a key feature highlighted in the README as a core goal.
Offers hooks for custom initialization and special handling, such as NumPy array equality checks, providing more control than basic alternatives.
Supports both modern (@define) and classic (@attr.s) APIs indefinitely, which can confuse users and increase the learning curve compared to simpler tools.
Requires installing a third-party package, adding an extra dependency that may not be acceptable in minimalist or strictly controlled environments.
For very simple classes with few attributes, the declarative syntax can introduce unnecessary abstraction and complexity over plain Python classes.
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.