dataclasses
@dataclass is a decorator from the standard library that writes __init__, a readable printout and == for a class from its list of fields.
Last updated: 30 Sep, 2026 · Python 3.14
A Ticket from Classes prints as a memory address, and two tickets with the same data are not equal. A dataclass fixes both and writes __init__ for you.
self.name in a plain class; only a default such as "AI" is also stored on the class.from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
profession: str = "AI"
person1 = Person("Krish", 31)
print(person1)
person1.profession = "Full stack developer"
print(person1)Person(name='Krish', age=31, profession='AI') Person(name='Krish', age=31, profession='Full stack developer')
There is no __init__ in the class, yet Person("Krish", 31) works, the default fills profession, and print shows every field. The Ticket below becomes a dataclass the same way.
Syntax:
from dataclasses import dataclass
@dataclass
class Name:
field: type
other: type = defaultDeclaring the ticket as a dataclass
from dataclasses import dataclass
@dataclass
class Ticket:
id: int
customer: str
text: str
category: str = "unsorted"
ticket = Ticket(1, "Asha", "I was charged twice for one order")
print(ticket)
print(ticket == Ticket(1, "Asha", "I was charged twice for one order"))Ticket(id=1, customer='Asha', text='I was charged twice for one order', category='unsorted') True
@dataclass is a decorator: a line starting with @ that changes the function or class right below it. This one reads the fields listed in the class and writes the __init__ that stores them, a readable printout and an equality check.
id: int names a field and the type it should hold. That annotation is a type hint, the subject of Type hints. category: str = "unsorted" gives a default, so it can be left out of the call.
Adding a method to a dataclass
@dataclass
class Ticket:
id: int
customer: str
text: str
category: str = "unsorted"
def categorise(self):
text = self.text.lower()
self.category = "billing" if "refund" in text else "other"
ticket = Ticket(3, "Chen", "Can I get a refund for the blue mug?")
ticket.categorise()
print(ticket)Ticket(id=3, customer='Chen', text='Can I get a refund for the blue mug?', category='billing')
"billing" if ... else "other" is an if and else squeezed into one value: the first when the condition is true, the second when not.
Passing values of the wrong type
ticket = Ticket("one", "Asha", 42)
print(ticket)Ticket(id='one', customer='Asha', text=42, category='unsorted')
A dataclass stores "one" and 42 without complaint, even though the hints say int and str. The hints describe; nothing enforces them. Pydantic models uses a class that does check.
Plain class vs dataclass
| Plain class | @dataclass | |
|---|---|---|
__init__ | You write it | Written from the fields |
print(ticket) | A memory address | Every field and its value |
== with the same data | False | True |
Where dataclasses show up in AI code
- Small records passed between steps: a result, a message, a config.
- Library code: many SDKs return dataclass-like objects that print their fields.
priority: int = 3 above text: str and Python raises a TypeError when the class is defined.Related
- Previous: Methods
- Next: Inheritance
- Reference: dataclasses in the Python docs
- Add a field
priority: int = 3and print a ticket. - Put a field without a default below one with a default, and read the error.
- Compare two tickets that differ only in
category.
You understood something today that you didn't yesterday.