Choosing Between Data Classes, Attrs, and Pydantic
Overview
Python developers often face a crossroads when modeling data structures. While standard classes work, they require significant boilerplate for initialization and comparisons.
Prerequisites
You should be comfortable with Python's basic syntax, specifically
Key Libraries & Tools
- Data Classes: The built-in Python module (PEP 557) for reducing boilerplate in data-heavy classes.
- Attrs: The spiritual predecessor to data classes, offering more granular control and features like converters.
- Pydantic: A data validation and settings management library that enforces type hints at runtime.
Code Walkthrough

The Standard Data Class
Data classes use decorators to automatically generate __init__ and __repr__ methods. They are lightweight and require no external installation.
from dataclasses import dataclass, field
@dataclass
class Product:
name: str
unit_price: int
shipping_weight: float = field(compare=False)
Here, the field(compare=False) flag allows us to exclude certain attributes when checking if two objects are equal.
Advanced Comparison with Attrs
Attrs provides more flexibility. You can transform data during comparison, such as ignoring case sensitivity in strings.
from attrs import define, field
@define
class Product:
name: str = field(eq=str.lower)
category: str = field(eq=str.lower)
By passing str.lower to the eq argument, Attrs ensures that "Mango" and "mango" are treated as the same product.
Strict Validation with Pydantic
Pydantic focuses on runtime enforcement. It uses inheritance from a BaseModel instead of decorators.
from pydantic import BaseModel, PositiveInt
class Product(BaseModel):
name: str
unit_price: PositiveInt
If you attempt to instantiate this class with a negative integer, Pydantic immediately raises a ValidationError.
Syntax Notes
Data Classes and Attrs prefer composition via decorators, keeping your class hierarchy clean. Pydantic relies on inheritance, which provides deep integration but can lead to namespace collisions if you aren't careful with method names.
Tips & Gotchas
Data Classes are tied to your

Fancy watching it?
Watch the full video and context