Live data from Hacker News

Attrs – the Python library everyone needs (2016)

glyph.twistedmatrix.com

11–20 of 115 posts

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

#11
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

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).

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

#13
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?

Named tuples have been around for a long time (since 2.6), whereas dataclasses are a relatively recent addition to the standard library (3.7).

Their differences are highlighted in the dataclasses PEP: https://www.python.org/dev/peps/pep-0557/#why-not-just-use-n...

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

#14
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

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 had a gun to my head to only use the standard library. I don’t know of a single Python project that uses dataclasses where Pydantic or Attrs would do (I’m sure they exist, but I’ve never run across it).

Dataclasses honestly seems very reactionary by the Python devs, since Attrs was getting so popular and used everywhere that it got a little embarrassing for Python that something so obviously needed in the language just wasn’t there. Those that weren’t using Attrs runtime validators often did something similar to Attrs by abusing NamedTuple with type hints. There were tons of “why isnt Attrs in the stdlib” comments, which is an annoying type of comment to make, but it happens. So they added dataclasses, but having all the many features that Attrs has isn’t a very standard-library-like approach, so we got… dataclasses. Like “look, it’s what you wanted, right!?”. Well no not really, thanks we’ll just keep using Attrs and then Pydantic

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

#15
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 think this was on HN at some point, but this article makes the case for why/when you’d want to use attrs rather than pydantic. https://threeofwands.com/why-i-use-attrs-instead-of-pydantic...

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

#17
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

Pydantic is useful if you're dealing with parsing unstructured (or sort of weakly untrusted) data. If you just want "things that feel like structs", dataclassess or attrs are going to be just as easy and more performant (and due to using decorators and not metaclasses, more capable of playing nicely with other things).

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

#19
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?

Namedtuples also behave like tuples, which is great when you want to incrementally turn tuples into classes but if you want an easy way of creating classes, it's probably not a good idea to have them behave like tuples. Plus dataclasses have more features.
Post reply on HN