0
0
PythonComparisonBeginner · 3 min read

Dataclass vs Regular Class in Python: Key Differences and Usage

A dataclass in Python automatically generates special methods like __init__ and __repr__ based on class attributes, reducing boilerplate. A regular class requires manually writing these methods, offering more control but more code.
⚖️

Quick Comparison

This table summarizes the main differences between dataclass and regular class in Python.

FactorDataclassRegular Class
Boilerplate CodeMinimal, auto-generated methodsManual, write all methods yourself
InitializationAuto __init__ from fieldsManual __init__ method needed
Immutability SupportSupports frozen=True for immutabilityManual implementation required
Comparison MethodsAuto __eq__, __lt__ etc. if requestedManual implementation needed
Use CaseBest for classes mainly storing dataBest for complex behavior and logic
Python VersionRequires Python 3.7+Works in all Python versions
⚖️

Key Differences

Dataclass is a decorator introduced in Python 3.7 that automatically adds special methods like __init__, __repr__, and __eq__ based on the class attributes you define. This means you write less code and get a clean, readable class mainly used for storing data.

In contrast, a regular class requires you to write these methods yourself, which gives you full control but increases the amount of code. You can customize behavior more freely but must handle all details manually.

Additionally, dataclasses support features like immutability with frozen=True and automatic ordering methods, which are not built-in for regular classes and need manual coding.

⚖️

Code Comparison

Here is how you create a simple class to store a person's name and age using a dataclass.

python
from dataclasses import dataclass

@dataclass
class Person:
    name: str
    age: int

p = Person("Alice", 30)
print(p)
print(p.name, p.age)
Output
Person(name='Alice', age=30) Alice 30
↔️

Regular Class Equivalent

This is the equivalent regular class doing the same task but with manual methods.

python
class Person:
    def __init__(self, name: str, age: int):
        self.name = name
        self.age = age

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

p = Person("Alice", 30)
print(p)
print(p.name, p.age)
Output
Person(name='Alice', age=30) Alice 30
🎯

When to Use Which

Choose dataclass when you want a simple, clean way to create classes mainly for storing data with less code and automatic features like comparison and immutability. It speeds up development and reduces errors in boilerplate.

Choose a regular class when you need full control over behavior, want to add complex methods, or support older Python versions. Regular classes are better for objects with significant logic beyond just data storage.

Key Takeaways

Dataclasses reduce boilerplate by auto-generating methods like __init__ and __repr__.
Regular classes require manual method definitions but offer more customization.
Use dataclasses for simple data containers and regular classes for complex behavior.
Dataclasses support immutability and comparison features out of the box.
Dataclasses need Python 3.7 or newer; regular classes work in all versions.