Skip to the content.
Dataclasses vs. Normal Classes in Python | AI Systems Design From Scratch

Connect with Amin Boulouma Official

🏠 Documentation Hub 📝 Engineering Blog 💻 GitHub Repository

Dataclasses vs. Normal Classes in Python

Amin Boulouma, Software Engineer

In modern Python development, the dataclass decorator (introduced in Python 3.7) has revolutionized how we handle objects that primarily exist to store data. Moving away from manual __init__ boilerplate and error-prone attribute assignments is a critical step toward writing cleaner, more professional code.

The Problem: The Boilerplate Tax

Standard classes often require repetitive code just to initialize attributes. This creates surface area for bugs, such as typos in variable names or inconsistent initializations.

The Old Way (Standard Class)

class User:
    def __init__(self, name, age, email):
        self.name = name
        self.age = age
        self.email = email

    def __repr__(self):
        return f"User(name={self.name}, age={self.age})"

The Modern Way: Dataclasses

Dataclasses leverage native type hinting to generate common methods (__init__, __repr__, __eq__) automatically, reducing your class definition to its core schema.

The Idiomatic Solution

from dataclasses import dataclass

@dataclass
class User:
    name: str
    age: int
    email: str

# Usage
user = User("Alice", 30, "alice@example.com")
print(user)

Why Dataclasses are Superior

  1. Reduced Boilerplate: You no longer need to write manual assignments.
  2. Type Hinting Integration: Because they rely on type hints, static analysis tools (like mypy or pyright) can catch bugs before you even run your code.
  3. Built-in Comparison: By default, dataclasses include an __eq__ method that compares the actual data values, not just the object identity in memory.
  4. Immutability: By adding @dataclass(frozen=True), you can easily create immutable data structures, which are safer in multi-threaded or complex state-management environments.

Comparison at a Glance

Feature Standard Class Dataclass
Initialization Manual (__init__) Automatic
Representation Manual (__repr__) Automatic
Comparison Manual (__eq__) Automatic
Type Safety Optional/Loose Native/Strict
Verbosity High Low

When to Stick with Standard Classes

Dataclasses are specifically for data-centric objects. You should continue using standard classes when:

Final Best Practice

Always treat your data objects as schemas. Using dataclasses with type hints turns your data structures into self-documenting contracts, making it significantly easier for your team to understand exactly what data a method expects and returns.

Connect with Amin Boulouma Official