| 1 |
<p align="center"> |
| 2 |
<a href="https://www.attrs.org/"> |
| 3 |
<picture> |
| 4 |
<source srcset="https://raw.githubusercontent.com/python-attrs/attrs/main/docs/_static/attrs_logo_white.svg" media="(prefers-color-scheme: dark)"> |
| 5 |
<img src="https://raw.githubusercontent.com/python-attrs/attrs/main/docs/_static/attrs_logo.svg" width="35%" alt="attrs" /> |
| 6 |
</picture> |
| 7 |
</a> |
| 8 |
</p> |
| 9 |
|
| 10 |
<p align="center"> |
| 11 |
<a href="https://www.attrs.org/en/stable/"><img src="https://img.shields.io/badge/Docs-RTD-black" alt="Documentation" /></a> |
| 12 |
<a href="https://github.com/python-attrs/attrs/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-C06524" alt="License: MIT" /></a> |
| 13 |
<a href="https://bestpractices.coreinfrastructure.org/projects/6482"><img src="https://bestpractices.coreinfrastructure.org/projects/6482/badge"></a> |
| 14 |
<a href="https://pypi.org/project/attrs/"><img src="https://img.shields.io/pypi/v/attrs" /></a> |
| 15 |
<a href="https://pepy.tech/project/attrs"><img src="https://static.pepy.tech/personalized-badge/attrs?period=month&units=international_system&left_color=grey&right_color=blue&left_text=Downloads%20/%20Month" alt="Downloads per month" /></a> |
| 16 |
<a href="https://zenodo.org/badge/latestdoi/29918975"><img src="https://zenodo.org/badge/29918975.svg" alt="DOI"></a> |
| 17 |
</p> |
| 18 |
|
| 19 |
<!-- teaser-begin --> |
| 20 |
|
| 21 |
*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](https://www.attrs.org/en/latest/glossary.html#term-dunder-methods)). |
| 22 |
[Trusted by NASA](https://docs.github.com/en/account-and-profile/setting-up-and-managing-your-github-profile/customizing-your-profile/personalizing-your-profile#list-of-qualifying-repositories-for-mars-2020-helicopter-contributor-achievement) for Mars missions since 2020! |
| 23 |
|
| 24 |
Its main goal is to help you to write **concise** and **correct** software without slowing down your code. |
| 25 |
|
| 26 |
|
| 27 |
## Sponsors |
| 28 |
|
| 29 |
*attrs* would not be possible without our [amazing sponsors](https://github.com/sponsors/hynek). |
| 30 |
Especially those generously supporting us at the *The Organization* tier and higher: |
| 31 |
|
| 32 |
<p align="center"> |
| 33 |
<a href="https://www.variomedia.de/"><img src="https://www.attrs.org/en/latest/_static/sponsors/Variomedia.svg" width="200" height="60" /></a> |
| 34 |
<a href="https://tidelift.com/subscription/pkg/pypi-attrs?utm_source=pypi-attrs&utm_medium=referral&utm_campaign=enterprise&utm_term=repo"><img src="https://www.attrs.org/en/latest/_static/sponsors/Tidelift.svg" width="200" height="60" /></a> |
| 35 |
<a href="https://filepreviews.io/"><img src="https://www.attrs.org/en/latest/_static/sponsors/FilePreviews.svg" width="200" height="60"/></a> |
| 36 |
</p> |
| 37 |
|
| 38 |
<p align="center"> |
| 39 |
<strong>Please consider <a href="https://github.com/sponsors/hynek">joining them</a> to help make <em>attrs</em>’s maintenance more sustainable!</strong> |
| 40 |
</p> |
| 41 |
|
| 42 |
<!-- teaser-end --> |
| 43 |
|
| 44 |
## Example |
| 45 |
|
| 46 |
*attrs* gives you a class decorator and a way to declaratively define the attributes on that class: |
| 47 |
|
| 48 |
<!-- code-begin --> |
| 49 |
|
| 50 |
```pycon |
| 51 |
>>> from attrs import asdict, define, make_class, Factory |
| 52 |
|
| 53 |
>>> @define |
| 54 |
... class SomeClass: |
| 55 |
... a_number: int = 42 |
| 56 |
... list_of_numbers: list[int] = Factory(list) |
| 57 |
... |
| 58 |
... def hard_math(self, another_number): |
| 59 |
... return self.a_number + sum(self.list_of_numbers) * another_number |
| 60 |
|
| 61 |
|
| 62 |
>>> sc = SomeClass(1, [1, 2, 3]) |
| 63 |
>>> sc |
| 64 |
SomeClass(a_number=1, list_of_numbers=[1, 2, 3]) |
| 65 |
|
| 66 |
>>> sc.hard_math(3) |
| 67 |
19 |
| 68 |
>>> sc == SomeClass(1, [1, 2, 3]) |
| 69 |
True |
| 70 |
>>> sc != SomeClass(2, [3, 2, 1]) |
| 71 |
True |
| 72 |
|
| 73 |
>>> asdict(sc) |
| 74 |
{'a_number': 1, 'list_of_numbers': [1, 2, 3]} |
| 75 |
|
| 76 |
>>> SomeClass() |
| 77 |
SomeClass(a_number=42, list_of_numbers=[]) |
| 78 |
|
| 79 |
>>> C = make_class("C", ["a", "b"]) |
| 80 |
>>> C("foo", "bar") |
| 81 |
C(a='foo', b='bar') |
| 82 |
``` |
| 83 |
|
| 84 |
After *declaring* your attributes, *attrs* gives you: |
| 85 |
|
| 86 |
- a concise and explicit overview of the class's attributes, |
| 87 |
- a nice human-readable `__repr__`, |
| 88 |
- equality-checking methods, |
| 89 |
- an initializer, |
| 90 |
- and much more, |
| 91 |
|
| 92 |
*without* writing dull boilerplate code again and again and *without* runtime performance penalties. |
| 93 |
|
| 94 |
**Hate type annotations**!? |
| 95 |
No problem! |
| 96 |
Types are entirely **optional** with *attrs*. |
| 97 |
Simply assign `attrs.field()` to the attributes instead of annotating them with types. |
| 98 |
|
| 99 |
--- |
| 100 |
|
| 101 |
This example uses *attrs*'s modern APIs that have been introduced in version 20.1.0, and the *attrs* package import name that has been added in version 21.3.0. |
| 102 |
The classic APIs (`@attr.s`, `attr.ib`, plus their serious-business aliases) and the `attr` package import name will remain **indefinitely**. |
| 103 |
|
| 104 |
Please check out [*On The Core API Names*](https://www.attrs.org/en/latest/names.html) for a more in-depth explanation. |
| 105 |
|
| 106 |
|
| 107 |
## Data Classes |
| 108 |
|
| 109 |
On the tin, *attrs* might remind you of `dataclasses` (and indeed, `dataclasses` [are a descendant](https://hynek.me/articles/import-attrs/) of *attrs*). |
| 110 |
In practice it does a lot more and is more flexible. |
| 111 |
For instance it allows you to define [special handling of NumPy arrays for equality checks](https://www.attrs.org/en/stable/comparison.html#customization), allows more ways to [plug into the initialization process](https://www.attrs.org/en/stable/init.html#hooking-yourself-into-initialization), and allows for stepping through the generated methods using a debugger. |
| 112 |
|
| 113 |
For more details, please refer to our [comparison page](https://www.attrs.org/en/stable/why.html#data-classes). |
| 114 |
|
| 115 |
|
| 116 |
## Project Information |
| 117 |
|
| 118 |
- [**Changelog**](https://www.attrs.org/en/stable/changelog.html) |
| 119 |
- [**Documentation**](https://www.attrs.org/) |
| 120 |
- [**PyPI**](https://pypi.org/project/attrs/) |
| 121 |
- [**Source Code**](https://github.com/python-attrs/attrs) |
| 122 |
- [**Contributing**](https://github.com/python-attrs/attrs/blob/main/.github/CONTRIBUTING.md) |
| 123 |
- [**Third-party Extensions**](https://github.com/python-attrs/attrs/wiki/Extensions-to-attrs) |
| 124 |
- **Get Help**: please use the `python-attrs` tag on [StackOverflow](https://stackoverflow.com/questions/tagged/python-attrs) |
| 125 |
|
| 126 |
|
| 127 |
### *attrs* for Enterprise |
| 128 |
|
| 129 |
Available as part of the Tidelift Subscription. |
| 130 |
|
| 131 |
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. |
| 132 |
Save time, reduce risk, and improve code health, while paying the maintainers of the exact packages you use. |
| 133 |
[Learn more.](https://tidelift.com/subscription/pkg/pypi-attrs?utm_source=pypi-attrs&utm_medium=referral&utm_campaign=enterprise&utm_term=repo) |