Re:I'm Switching to Python and Actually Liking It
by whinvik on Wednesday July 16, 2025 @01:55PM
(#44585089)
Out of curiosity, why would I use Dataclass vs a Pydantic Basemodel. If we did not have a PyDantic dependency I could imagine wanting to use Dataclass. But if I have it, why not use everywhere?
[ Parent
| on HN ]
Re:I'm Switching to Python and Actually Liking It
by rsyring on Wednesday July 16, 2025 @02:09PM
(#44585228)
To help answer your question, here's a detailed comparison from the attrs project:
https://www.attrs.org/en/stable/why.html
There's some obvious potential for bias there, but I thought most of the arguments were well reasoned.
Edit: found another that I thought was helpful: https://threeofwands.com/why-i-use-attrs-instead-of-pydantic...
Re:I'm Switching to Python and Actually Liking It
by tauroid on Wednesday July 16, 2025 @04:04PM
(#44586241)
Why would I use a Pydantic model when `TypeAdapter(MyDataclass)` exists?
Re:I'm Switching to Python and Actually Liking It
by intalentive on Wednesday July 16, 2025 @02:32PM
(#44585431)
Performance hit from data validation at construction time. I like msgspec which is much leaner and faster.
Re:I'm Switching to Python and Actually Liking It
by afiodorov on Wednesday July 16, 2025 @03:38PM
(#44586010)
My rule of the thumb is that Dataclasses are for compile-time type-checking, pydantic classes are for run time type checking.
Re:I'm Switching to Python and Actually Liking It
by maleldil on Wednesday July 16, 2025 @03:06PM
(#44585740)
Unnecessary baggage if you don't need validation or serialisation.
My rule of thumb is to use Pydantic if I need serialisation, otherwise I default to dataclasses.
Re:I'm Switching to Python and Actually Liking It
by globular-toast on Thursday July 17, 2025 @02:51AM
(#44590469)
This and not just for the performance reasons. A Pydantic model represents an I/O boundary of a program. This conveys a lot of information to a programmer reading the code. It would be quite misleading to find it's actually only passed around internally and never used for I/O. A bit like using an int type to store a Boolean value.
On the other hand if I see a dataclass I can tell what it's purpose is by whether it's frozen or not etc.
Always strive for self-documenting code.
Re:I'm Switching to Python and Actually Liking It
by lysecret on Wednesday July 16, 2025 @02:40PM
(#44585508)
Main reason is (used to be) performance. But since pydantic 2.0 it’s not an issue anymore I default to use pydantic for everything.
Re:I'm Switching to Python and Actually Liking It
by adammarples on Wednesday July 16, 2025 @03:33PM
(#44585964)
The Chad answer is to use pydantic.dataclass
Re:I'm Switching to Python and Actually Liking It
by meander_water on Wednesday July 16, 2025 @07:03PM
(#44587776)
One explicit difference is that dataclasses don't support validating nested objects.
I'd use it for passing flat structures as function args rather than a massive list of individual args.