Python24 min read

Python Dataclasses

Write clean data-holding classes using dataclasses: auto __init__, defaults, immutability, sorting, and validation with __post_init__.

David Miller
August 13, 2025
3.6k147

Dataclasses make it easy to create classes that mainly store data.

      Without dataclasses, you write a lot of boilerplate:
      - __init__
      - __repr__
      - comparisons
      
      With dataclasses, Python generates these for you automatically.
      
      ## Basic dataclass
      
      ```python
      from dataclasses import dataclass
      
      @dataclass
      class Person:
          name: str
          age: int
          city: str
      
      p = Person("Tom", 25, "Austin")
      print(p)
      ```
      
      Expected output:
      ```
      Person(name='Tom', age=25, city='Austin')
      ```
      
      ## Default values
      
      ```python
      from dataclasses import dataclass
      
      @dataclass
      class Product:
          name: str
          price: float
          quantity: int = 0
      
      item = Product("Laptop", 999.99)
      print(item)
      ```
      
      ## Frozen dataclass (immutable)
      
      ```python
      from dataclasses import dataclass
      
      @dataclass(frozen=True)
      class Point:
          x: int
          y: int
      
      pt = Point(10, 20)
      print(pt)
      # pt.x = 99  # would fail
      ```
      
      ## __post_init__ for validation or computed fields
      
      ```python
      from dataclasses import dataclass, field
      
      @dataclass
      class Rectangle:
          width: float
          height: float
          area: float = field(init=False)
      
          def __post_init__(self):
              if self.width <= 0 or self.height <= 0:
                  raise ValueError("Width and height must be positive")
              self.area = self.width * self.height
      
      r = Rectangle(10, 5)
      print(r.area)
      ```
      
      ## Sorting dataclasses
      
      ```python
      from dataclasses import dataclass
      
      @dataclass(order=True)
      class Student:
          grade: int
          name: str
      
      students = [
          Student(85, "Tom"),
          Student(92, "Sarah"),
          Student(78, "Mike")
      ]
      
      students.sort(reverse=True)
      for s in students:
          print(s)
      ```
      
      ## Graph: dataclass benefit
      
      ```mermaid
      flowchart LR
        A[Regular class] --> B[Write __init__]
        A --> C[Write __repr__]
        A --> D[Write comparisons]
        E[Dataclass] --> F[Auto-generates boilerplate]
      ```
      
      ## Key points
      
      - Best for data containers
      - Reduces boilerplate code
      - Use frozen=True for immutability
      - Use __post_init__ for validation
      
#Python#Advanced#OOP