Python for AIPython 3.10+ · Pydantic 2.12
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
34 small wins to finish your pathNext lesson →

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.

A Person dataclass with a default · from Learn How Python Data Classes Can Save Your Time In Projects · 3:36 to 8:17
The video calls the fields class variables. Each field becomes an attribute of every object made from the class, like self.name in a plain class; only a default such as "AI" is also stored on the class.
ExampleFrom the video, run here
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)

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:

python
from dataclasses import dataclass

@dataclass
class Name:
    field: type
    other: type = default

Declaring the ticket as a dataclass

Example
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"))

@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

Example
@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)

"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

Example
ticket = Ticket("one", "Asha", 42)
print(ticket)

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 itWritten from the fields
print(ticket)A memory addressEvery field and its value
== with the same dataFalseTrue

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.
Watch out. A field without a default cannot come after one with a default. Put priority: int = 3 above text: str and Python raises a TypeError when the class is defined.
Try it yourself
  • Add a field priority: int = 3 and 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.
PreviousMethods

You understood something today that you didn't yesterday.