Skip to main content

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

Full changelog.

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?

Release files for attrs 21.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for attrs 21.2.0
File Size Uploaded
attrs-21.2.0.tar.gz 184.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for attrs 21.2.0
File Interpreter ABI Platform
attrs-21.2.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 238.4 kB

Release files / attrs-21.2.0.tar.gz

Download URL attrs-21.2.0.tar.gz
Size 184.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ef6aaac3ca6cd92904cdd0d83f629a15f18053ec84e6432106f7a4d04ae4f5fb
BLAKE2b-256 checksum
How to use checksums
edd63ebca4ca65157c12bd08a63e20ac0bdc21ac7f3694040711f9fd073c0ffb
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / attrs-21.2.0-py2.py3-none-any.whl

Download URL attrs-21.2.0-py2.py3-none-any.whl
Size 53.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
149e90d6d8ac20db7a955ad60cf0e6881a3f20d37096140088356da6c716b0b1
BLAKE2b-256 checksum
How to use checksums
20a9ba6f1cd1a1517ff022b35acd6a7e4246371dfab08b8e42b829b6d07913cc
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

26.1.0

2 release files

25.3.0

2 release files

25.2.0

2 release files

25.1.0

2 release files

24.3.0

2 release files

23.2.0

2 release files

23.1.0

2 release files

22.2.0

2 release files

22.1.0

2 release files

21.4.0

2 release files

21.3.0

2 release files

This release

21.2.0 This release

2 release files

20.1.0

2 release files

19.3.0

2 release files

17.4.0

2 release files

17.2.0

2 release files

17.1.0

2 release files

16.3.0

2 release files

16.2.0

2 release files

16.1.0

2 release files

16.0.0

2 release files

15.1.0

2 release files

15.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page