Live data from Hacker News

Attrs – the Python library everyone needs (2016)

glyph.twistedmatrix.com

21–30 of 115 posts

Re: Attrs – the Python library everyone needs (2016)

#22
Costs: Depending on a relatively unknown library. Using arcane class decorators and unusual syntactic constructs: @attr.s and x = attr.ib() (a pun?).

Benefits: Saving at best 10-15 lines of boilerplate per data class. Much less if namedtuple works for you.

If you want to save lines in __init__ you can write "for k, v in locals().items(): setattr(self, k, v)". But you shouldn't.

Edit: Forgot to add to the most important cost: Magic. You don't need to know a lot of Python to understand how the standard self.x = x initialization works. However, you do need to understand a lot of Python internals to grok x = attr.ib().

Re: Attrs – the Python library everyone needs (2016)

#24
post #7
post #2

see also "dataclasses" since python 3.7

Not a python guy, so confused as to why a thing called namedtuple behaves like dataclasses, what are their different usecases?

From a users perspective data classes look kind of like a C struct and in particular include type annotations so fit well with type checkers. They also allow for default values and give more control over generating equality, hash, string and initialisation methods.

Comparatively named tuples are an older language feature which essentially allow you to define named accessors for tuple elements. IIRC, these days you can also define type annotations for them.

Their use case essentially overlap. Personally I much prefer data classes.

Re: Attrs – the Python library everyone needs (2016)

#25
post #4

IMO Pydantic is way more ergonomic, has great defaults, and easier to bend to your will when you want to use it a little differently. Lots of love to Attrs, which is a great library and is a component of a lot of great software. It was my go-to library for years before Pydantic matured, but I think a lot of people have rightly started to move on to Pydantic, particularly with the popularity of FastAPI

I'd say the opposite. Specifically, Pydantic tries to do everything, and as a result (partly b/c they favor base classes over higher order functions), it isn't as composable as attrs is.

I've done some truly amazing things with attrs because of that composability. If I'd wanted the same things with Pydantic, it would have had to be a feature request.

Re: Attrs – the Python library everyone needs (2016)

#26

Earlier quoted context omitted.

Do you have concern with speed or memory footprint of pydantic compared to the rest (attrs, dataclasses etc)? Pydantic seems insistent on parsing/validating the types at runtime (which makes good sense for something like FastAPI).

We always used attrs with the runtime type validators anyway. Getting those types checked in Python was way more valuable to my teams than the minor boilerplate reduction. If you’re worried about the performance hit of extra crap happening at runtime… dear lord use another programming language. Dataclasses is just… meh. Pydantic and Attrs just have so many great features, I would never use dataclasses unless someone…

Thanks. You do make a lot of good points.

Attrs just has the features I need for now. It certainly feel a touch verbose but I’m happy to pay the price.

Re: Attrs – the Python library everyone needs (2016)

#28
post #6

my_point3d = (1.0, 2.5, 7.2) Voila!

This is explicitly called out in the article. What does your code do when I try my_point3d.x ?

you can do that with namedtuples too; the problem is that tuples are immutable, so you cannot say e.g. `my_point3d.x = 10`

Re: Attrs – the Python library everyone needs (2016)

#29
post #2

see also "dataclasses" since python 3.7

Came here to say this, dataclasses have been super helpful for a big part of the pain point highlighted by the author. More often than not, that is enough for me.

Yes. There needs to be a very good reason for me to pull in a third party library (in this day and age, given supply chain attacks, etc.). I don’t see what Attrs gives me that dataclasses does not.
Post reply on HN