How to Use Python’s dataclass for Cleaner and Faster Code

Are writing classes in Python sometimes too much work? You have to usually define an  _init_ method, or  _repr_ or _eq_method, and suddenly your class is filled with code! Well, guess what? Python’s dataclass is here to save the day!

While using something called a decorator, you get automatic methods for initialization, representation, and comparison. This makes your code cleaner, faster, and easier to read.

dataclass

A dataclass is just a Python class with built-in automation. When the @dataclass decorator is used, Python automatically generates:

  • An _init_ method to initialize attributes
  • _repr_ method for easy debugging
  • An _eq_ method for object comparison
  • Some optional features like ordering and immutability
    The point is that instead of writing all this manually, Python does it for you!

In Python, __repr__and __eq__ are special methods (also called dunder methods) that help define how objects behave.

Here’s an easy to understand example:

Without dataclass =>

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

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

p = Person("Nilin", 23)
print(p)  # person(name=Nilin, age=23)

With =>

from dataclasses import dataclass

@dataclass
class Person:
    name: str
    age: int

p = Person("Nilin", 23)
print(p)  # Person(name='Nilin', age=23)

As you can see the number of lines have reduced which makes the script way more readable.

 

Here are some more functions achieved:

Default Values

@dataclass
class Car:
    brand: str
    model: str
    year: int = 2025 # Default value

c = Car("Toyota", "Corolla")
print(c)  # Car(brand='Toyota', model='Corolla', year=2025)

If you don’t add a year it defaults to 2025.

Immutability

@dataclass(frozen=True)
class Point:
    x: int
    y: int

p = Point(2, 3)
p.x = 5  # Can't modify frozen it gives error

Immutable dataclasses are called frozen. It makes it read-only.

Auto Sorting

@dataclass(order=True)
class Student:
    name: str
    grade: int

s1 = Student("Nilin", 95)
s2 = Student("Rose", 80)

print(s1 > s2)  # True (because 95 > 80)

students = [Student("A", 90), Student("B", 80), Student("C", 85)]
print(sorted(students)) 
# [Student(name='B', grade=80), Student(name='C', grade=85), Student(name='A', grade=90)]

Customisation

from dataclasses import dataclass, field

@dataclass
class Employee:
    name: str
    salary: int = field(default=5000, metadata={"description": "salary"})

USES

Benefits:

dataclass benefits
dataclass benefits

Dataclasses also may not be the best option sometimes. Here are a few cases:

  • custom methods beyond storing data.
  • complex inheritance (not handled it well).
  • In some cases manually optimized classes can be faster.
    For other cases , dataclass is usually a great choice!

Thus  your code is cleaner, easier to read, and more efficient ;all while saving you from a lot of typing.

 Less typing, more coding, and better performance!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top