top | item 40861397

(no title)

lolpanda | 1 year ago

what are the pros and cons with pydantic models vs data classes? i like the runtime validation in pydantic models. the pattern i landed on is that 1) only use primitive data types such as str, int, pydantic models and pandas dataframes to present data. 2) no classes with methods, no polymorphism, only use module functions.

discuss

order

martinky24|1 year ago

Pydantic models are significantly more heavyweight than dataclasses -- They serve as validators as opposed to dataclasses, which are primarily just tools for managing structured data. Both are important problems, but they are different.

The power of Pydantic models comes with benefits in terms of what they're able to do out of the box. But this comes at a significant cost of speed, which does matter for some applications. There are plenty of applications where it simply doesn't make sense to validate types every time you stand up a class.

Sure you can turn some of that off with Pydantic, etc, etc, but the fact that dataclasses don't require a third party install and are efficient and easy out of the box makes them useful and non-duplicative.