README.rst
1.. image:: http://www.attrs.org/en/latest/_static/attrs_logo.png
2 :alt: attrs Logo
3
4======================================
5``attrs``: Classes Without Boilerplate
6======================================
7
8.. image:: https://readthedocs.org/projects/attrs/badge/?version=stable
9 :target: http://www.attrs.org/en/stable/?badge=stable
10 :alt: Documentation Status
11
12.. image:: https://travis-ci.org/python-attrs/attrs.svg?branch=master
13 :target: https://travis-ci.org/python-attrs/attrs
14 :alt: CI Status
15
16.. image:: https://codecov.io/github/python-attrs/attrs/branch/master/graph/badge.svg
17 :target: https://codecov.io/github/python-attrs/attrs
18 :alt: Test Coverage
19
20.. teaser-begin
21
22``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 <https://nedbatchelder.com/blog/200605/dunder.html>`_ methods).
23
24Its main goal is to help you to write **concise** and **correct** software without slowing down your code.
25
26.. -spiel-end-
27
28For that, it gives you a class decorator and a way to declaratively define the attributes on that class:
29
30.. -code-begin-
31
32.. code-block:: pycon
33
34 >>> import attr
35
36 >>> @attr.s
37 ... class SomeClass(object):
38 ... a_number = attr.ib(default=42)
39 ... list_of_numbers = attr.ib(default=attr.Factory(list))
40 ...
41 ... def hard_math(self, another_number):
42 ... return self.a_number + sum(self.list_of_numbers) * another_number
43
44
45 >>> sc = SomeClass(1, [1, 2, 3])
46 >>> sc
47 SomeClass(a_number=1, list_of_numbers=[1, 2, 3])
48
49 >>> sc.hard_math(3)
50 19
51 >>> sc == SomeClass(1, [1, 2, 3])
52 True
53 >>> sc != SomeClass(2, [3, 2, 1])
54 True
55
56 >>> attr.asdict(sc)
57 {'a_number': 1, 'list_of_numbers': [1, 2, 3]}
58
59 >>> SomeClass()
60 SomeClass(a_number=42, list_of_numbers=[])
61
62 >>> C = attr.make_class("C", ["a", "b"])
63 >>> C("foo", "bar")
64 C(a='foo', b='bar')
65
66
67After *declaring* your attributes ``attrs`` gives you:
68
69- a concise and explicit overview of the class's attributes,
70- a nice human-readable ``__repr__``,
71- a complete set of comparison methods,
72- an initializer,
73- and much more,
74
75*without* writing dull boilerplate code again and again and *without* runtime performance penalties.
76
77This gives you the power to use actual classes with actual types in your code instead of confusing ``tuple``\ s or `confusingly behaving <http://www.attrs.org/en/stable/why.html#namedtuples>`_ ``namedtuple``\ s.
78Which in turn encourages you to write *small classes* that do `one thing well <https://www.destroyallsoftware.com/talks/boundaries>`_.
79Never again violate the `single responsibility principle <https://en.wikipedia.org/wiki/Single_responsibility_principle>`_ just because implementing ``__init__`` et al is a painful drag.
80
81
82.. -testimonials-
83
84Testimonials
85============
86
87**Amber Hawkie Brown**, Twisted Release Manager and Computer Owl:
88
89 Writing a fully-functional class using attrs takes me less time than writing this testimonial.
90
91
92**Glyph Lefkowitz**, creator of `Twisted <https://twistedmatrix.com/>`_, `Automat <https://pypi.python.org/pypi/Automat>`_, and other open source software, in `The One Python Library Everyone Needs <https://glyph.twistedmatrix.com/2016/08/attrs.html>`_:
93
94 I’m looking forward to is being able to program in Python-with-attrs everywhere.
95 It exerts a subtle, but positive, design influence in all the codebases I’ve see it used in.
96
97
98**Kenneth Reitz**, author of `requests <http://www.python-requests.org/>`_, Python Overlord at Heroku, `on paper no less <https://twitter.com/hynek/status/866817877650751488>`_:
99
100 attrs—classes for humans. I like it.
101
102
103**Łukasz Langa**, prolific CPython core developer and Production Engineer at Facebook:
104
105 I'm increasingly digging your attr.ocity. Good job!
106
107
108.. -end-
109
110.. -project-information-
111
112Getting Help
113============
114
115Please use the ``python-attrs`` tag on `StackOverflow <https://stackoverflow.com/questions/tagged/python-attrs>`_ to get help.
116
117Answering questions of your fellow developers is also great way to help the project!
118
119
120Project Information
121===================
122
123``attrs`` is released under the `MIT <https://choosealicense.com/licenses/mit/>`_ license,
124its documentation lives at `Read the Docs <http://www.attrs.org/>`_,
125the code on `GitHub <https://github.com/python-attrs/attrs>`_,
126and the latest release on `PyPI <https://pypi.org/project/attrs/>`_.
127It’s rigorously tested on Python 2.7, 3.4+, and PyPy.
128
129We collect information on **third-party extensions** in our `wiki <https://github.com/python-attrs/attrs/wiki/Extensions-to-attrs>`_.
130Feel free to browse and add your own!
131
132If you'd like to contribute to ``attrs`` you're most welcome and we've written `a little guide <http://www.attrs.org/en/latest/contributing.html>`_ to get you started!
133