Skip to main content

How to Create a Data Class in Python with @dataclass

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A data class is a Python class whose job is to hold a few values together, such as a book's title and page count. Put @dataclass on top of a class, list its fields with type hints, and Python writes the boring methods for you. This guide makes one on Python 3.15.2.

Syntax​

Syntax
from dataclasses import dataclass


@dataclass
class <ClassName>:
<field>: <type>
<field>: <type>
PartWhat it means
from dataclasses import dataclassLoads the decorator from Python's standard library. Nothing to install
@dataclassTurns the class below it into a data class
<field>: <type>One value the class holds, with its type hint. Every line like this becomes a field

The dataclasses documentation says the decorator "is used to add generated special methods to classes", and that "A field is defined as a class variable that has a type annotation."

The Example​

A Book class holds a title and a number of pages. The script makes one book, prints it, reads its fields, and compares it with a second book that has the same values.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

book.py
from dataclasses import dataclass


@dataclass
class Book:
title: str
pages: int


book = Book(title="Dune", pages=412)
print(book)
print(book.title, book.pages)
print(book == Book(title="Dune", pages=412))

The book.py script in a macOS-style code window: a Book data class with a title field of type str and a pages field of type int, then a book created with the title Dune and 412 pages, printed whole, printed field by field, and compared with an identical book.

Step 2: Run It​

Terminal
uv run python book.py
Output
Book(title='Dune', pages=412)
Dune 412
True

The terminal after uv run python book.py: Book(title='Dune', pages=412), then Dune 412, then True.

The class body has no methods, yet three things worked:

LineWhat @dataclass wrote for you
Book(title="Dune", pages=412)An __init__ method that takes one argument per field, in the order you listed them
print(book)A __repr__ method, so the book prints its class name and every field
book == Book(...)An __eq__ method that compares the fields, so two books with the same values are equal

A plain class without @dataclass would print something like <__main__.Book object at 0x…>, and the comparison would be False, because two separate objects are only equal to themselves. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out a field when you create the object: Book(title="Dune")TypeError: Book.__init__() missing 1 required positional argument: 'pages'Pass every field, or give the field a default value in the class, such as pages: int = 0
Writing a field without a type hint: shelf = "A"It isn't a field. It doesn't appear in the print-out or in __init__, because a field "is defined as a class variable that has a type annotation"Add a type hint: shelf: str = "A"
Expecting @dataclass to check the typesBook(title="Dune", pages="many") is accepted: the docs say nothing in @dataclass "examines the type specified in the variable annotation"Check values yourself, or use a validation library such as pydantic

FAQ​

Can a data class have its own methods?​

Yes. Add them under the fields like in any class. @dataclass only adds the methods you didn't write yourself.

How do I make the object read-only?​

Use @dataclass(frozen=True). Assigning to a field then raises an error instead of changing it.

What's the difference between a data class and a dictionary?​

A dictionary such as {"title": "Dune", "pages": 412} accepts any keys. A data class has a fixed list of fields, so a typo in a field name is an error instead of a new key, and editors can suggest the field names as you type.

Conclusion​

Four lines under @dataclass gave Book a constructor, a readable print-out and a working ==. List the fields with type hints, and Python writes the rest.

Additional Resources​