
Creating Python Classes the Easy Way with Dataclasses
In Python, everything is an object. If you want to make your own custom objects with properties and methods, you typically define a class. But writing classes can involve a lot of repetitive boilerplate—copying constructor arguments to object properties, writing comparison operators, or creating __repr__ methods. This can quickly become tedious and error-prone, especially when you have multiple classes like Book, Bookshelf, Library, and so on.
Enter dataclasses, introduced in Python 3.7 (and backported to 3.6). Dataclasses simplify class creation by automatically generating the boilerplate code that’s usually needed for initialization, representation, and comparison. This lets you focus on the essential properties of your class without repetitive manual coding.
Consider a conventional Python class:
class Book:
'''Object for tracking physical books in a collection.'''
def __init__(self, name: str, weight: float, shelf_id: int = 0):
self.name = name
self.weight = weight
self.shelf_id = shelf_id
def __repr__(self):
return (f"Book(name={self.name!r}, weight={self.weight!r}, shelf_id={self.shelf_id!r})")
Here, you have to manually copy constructor arguments to object properties and write the __repr__ method. Doing this repeatedly across multiple classes increases the chances of mistakes and bloats your code.
With a dataclass, the same class can be written much more concisely:
from dataclasses import dataclass
@dataclass
class Book:
'''Object for tracking physical books in a collection.'''
name: str
weight: float
shelf_id: int = 0
By using the @dataclass decorator, Python automatically generates the __init__, __repr__, __eq__, and other common methods for you. Type annotations are preserved, so linters can check that the right kinds of variables are passed to the constructor. You can still override any automatically generated methods if needed, but for most cases, the dataclass handles everything for you.
Functionally, dataclasses behave like regular classes. There’s a tiny one-time performance cost when the dataclass is created, but no ongoing performance penalty. Using dataclasses allows you to write cleaner, more maintainable Python code while eliminating much of the repetitive boilerplate.

