3 ms·
For my general uses, pydantic and dataclasses are very similar, and I prefer pydantic over dataclass for the following reasons: 1. pydantic by default ignores
by leoff 4y ago
For my general uses, pydantic and dataclasses are very similar, and I prefer pydantic over dataclass for the following reasons:
1. pydantic by default ignores extra fields - it's useful when I make an API call and want to extract and validate only certain fields from the response, while dataclasses throws errors if you don't specify all fields, and this behavior can't be disabled
2. pydantic is more customizable - I can overwrite the BaseModel if I have a custom need
3. pydantic seems to integrate better with python typing - I'm not sure how to explain this one, but it feels more natural and dynamic
I'm not sure what the performance concerns are, though
- eru 4y agoThere's also dataclassy, which stays closer to the standard library dataclasses while improving on them.
- lysecret 4y agoPydantic is much much much slower than normal dataclasses. However, they have to be because they do validation and dataclasses do not. So, I use normal dataclasses for internal constructs, which need to be fast and pydantic for anything that comes externally, can't be trusted and has to be validated.
- stevesimmons 4y agoPydantic models also have a model.construct(*src) form which skips validation. According to Pydantic's docs [1], this makes it 30x faster. [1] https://docs.pydantic.dev/usage/models/#creating-models-without-validation https://docs.pydantic.dev/usage/models/#creating-models-with...
- animuchan 4y agoWhat I seriously love about pydantic is the ability to just write the default value of `tags: list[str] = []` and not worry about all of the instances of the class sharing the same single instance of the default list.
- thanatropism 4y agoThe intent of pydantic is to be a parser of (possibly recursive, with composition of classes) key-value "languages". It could, in principle, evolve in a completely different direction than dataclasses/msgpack/dataclassy, etc. This is like -- there are many libraries that do PCA, from sklearn to statsmodels to plain scipy to genomics tools. But in a glue-language workflow you should choose a tool for its semantics.