What does HackerNews think of attrs?

Python Classes Without Boilerplate

Language: Python

#79 in Python
This is great recommendation, you can get very far by reading well written code. These have very well written Python, exposing much of the language expressiveness:

- https://github.com/python-attrs/attrs

- https://github.com/mahmoud/glom

- https://github.com/pytoolz/toolz

- https://github.com/Suor/funcy

- https://github.com/dabeaz/curio

  ps: dabaez has great educational content on writing idiomatic python, definitely worth checking out
An honorable mention of norvig's classic essays, which got me into python while I was in college, over 12y ago: https://norvig.com/spell-correct.html.

Aside from reading code, _writing_ something that you know it exist (e.g glom) and then comparing it to how others have done it is also a great learning experience.

Data Classes are a cool feature, useful, and a superior alternative to namedtuple.

But while this was brewing, attrs ate their lunch. Data Classes just don't go far enough compared to what attrs [1] has provided for a while now. If there's a compelling reason to switch, I haven't found it yet.

Can I use Data Classes in a library supporting Python 2.7, 3.4-3.7? I don't think the answer is yes today.

[1]: https://github.com/python-attrs/attrs

Take a look at https://github.com/python-attrs/attrs which will let you do something like

    import attr

    @attr.s
    class Key(object):
         modifier = attr.ib()
         key = attr.ib()
         function = attr.ib()
         argument = attr.ib()

    keys = [
        Key(MODKEY,           XK_p,      spawn, {'v': dmenucmd}),
        Key(MODKEY|ShiftMask, XK_Return, spawn, {'v': termcmd}),
        # ...
    ]
which gets you the concise and readable syntax of C structs, while keeping these things as actual attributes as if they were classes (e.g., [key.modifier for key in keys] will work).

And you get designated initializers via Python's usual keyword-argument syntax, the ability to specify attr.ib(default=...), and a few other things.

Much of the code in the post could be simplified by using the awesome `attrs` package [1].

Or if / when PEP 557 gets adopted.

[1]: https://github.com/python-attrs/attrs

[2]: https://www.python.org/dev/peps/pep-0557/

Agreed, I use the fact that "namedtuples are tuples" constantly. For one thing, you can pass them to 3rd party libraries that take tuples while maintaining readability on your end. I would be horrified if they dropped index access. It would break everything.

I should say though, for simple POJO-style classes @attr.s (https://github.com/python-attrs/attrs) has replaced 90% of my namedtuple usage.