Classes Without Boilerplate
Project description
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.
For that, it gives you a class decorator and a way to declaratively define the attributes on that class:
>>> import attr
>>> @attr.s
... class SomeClass(object):
... a_number = attr.ib(default=42)
... list_of_numbers = attr.ib(factory=list)
...
... def hard_math(self, another_number):
... return self.a_number + sum(self.list_of_numbers) * another_number
>>> sc = SomeClass(1, [1, 2, 3])
>>> sc
SomeClass(a_number=1, list_of_numbers=[1, 2, 3])
>>> sc.hard_math(3)
19
>>> sc == SomeClass(1, [1, 2, 3])
True
>>> sc != SomeClass(2, [3, 2, 1])
True
>>> attr.asdict(sc)
{'a_number': 1, 'list_of_numbers': [1, 2, 3]}
>>> SomeClass()
SomeClass(a_number=42, list_of_numbers=[])
>>> C = attr.make_class("C", ["a", "b"])
>>> C("foo", "bar")
C(a='foo', b='bar')
After declaring your attributes attrs gives you:
a concise and explicit overview of the class’s attributes,
a nice human-readable __repr__,
a complete set of comparison methods (equality and ordering),
an initializer,
and much more,
without writing dull boilerplate code again and again and without runtime performance penalties.
On Python 3.6 and later, you can often even drop the calls to attr.ib() by using type annotations.
This gives you the power to use actual classes with actual types in your code instead of confusing tuples or confusingly behaving namedtuples. Which in turn encourages you to write small classes that do one thing well. Never again violate the single responsibility principle just because implementing __init__ et al is a painful drag.
Getting Help
Please use the python-attrs tag on StackOverflow to get help.
Answering questions of your fellow developers is also a great way to help the project!
Project Information
attrs is released under the MIT license, its documentation lives at Read the Docs, the code on GitHub, and the latest release on PyPI. It’s rigorously tested on Python 2.7, 3.5+, and PyPy.
We collect information on third-party extensions in our wiki. Feel free to browse and add your own!
If you’d like to contribute to attrs you’re most welcome and we’ve written a little guide to get you started!
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. Learn more.
Release Information
21.2.0 (2021-05-07)
Backward-incompatible Changes
We had to revert the recursive feature for attr.evolve() because it broke some use-cases – sorry! #806
Python 3.4 is now blocked using packaging metadata because attrs can’t be imported on it anymore. To ensure that 3.4 users can keep installing attrs easily, we will yank 21.1.0 from PyPI. This has no consequences if you pin attrs to 21.1.0. #807
Credits
attrs is written and maintained by Hynek Schlawack.
The development is kindly supported by Variomedia AG.
A full list of contributors can be found in GitHub’s overview.
It’s the spiritual successor of characteristic and aspires to fix some of it clunkiness and unfortunate decisions. Both were inspired by Twisted’s FancyEqMixin but both are implemented using class decorators because subclassing is bad for you, m’kay?
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file attrs-21.2.0.tar.gz
.
File metadata
- Download URL: attrs-21.2.0.tar.gz
- Upload date:
- Size: 184.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef6aaac3ca6cd92904cdd0d83f629a15f18053ec84e6432106f7a4d04ae4f5fb |
|
MD5 | 06af884070d9180694becdb106e5cd65 |
|
BLAKE2b-256 | edd63ebca4ca65157c12bd08a63e20ac0bdc21ac7f3694040711f9fd073c0ffb |
File details
Details for the file attrs-21.2.0-py2.py3-none-any.whl
.
File metadata
- Download URL: attrs-21.2.0-py2.py3-none-any.whl
- Upload date:
- Size: 53.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 149e90d6d8ac20db7a955ad60cf0e6881a3f20d37096140088356da6c716b0b1 |
|
MD5 | 50e772bf1a75df648734eb0498383df6 |
|
BLAKE2b-256 | 20a9ba6f1cd1a1517ff022b35acd6a7e4246371dfab08b8e42b829b6d07913cc |