Someone should tell this person about dataclasses
Attrs – the Python library everyone needs (2016)
21–30 of 115 posts
Re: Attrs – the Python library everyone needs (2016)
#22Benefits: 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)
#23Re: Attrs – the Python library everyone needs (2016)
#24see 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?
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)
#25IMO 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'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)
#26Earlier 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…
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)
#27Re: Attrs – the Python library everyone needs (2016)
#28Re: Attrs – the Python library everyone needs (2016)
#29see 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.