Skip to content

Latest commit

 

History

History
90 lines (71 loc) · 1.93 KB

File metadata and controls

90 lines (71 loc) · 1.93 KB

+++ slug = "dataclasses" title = "Dataclasses" section = "Classes" summary = "dataclass generates common class methods for data containers." doc_path = "/library/dataclasses.html" see_also = [ "structured-data-shapes", "classes", "type-hints", ] +++

dataclass is a standard-library decorator for classes that mainly store data. It generates methods such as __init__ and __repr__ from type-annotated fields.

Dataclasses reduce boilerplate while keeping classes explicit. They are a good fit for simple records, configuration objects, and values passed between layers.

Type annotations define fields. Defaults work like normal class attributes and appear in the generated initializer.

:::program

from dataclasses import dataclass

@dataclass
class User:
    name: str
    active: bool = True

user = User("Ada")
print(user)
print(user.name)

inactive = User("Guido", active=False)
print(inactive)
print(inactive.active)

:::

:::cell A dataclass uses annotations to define fields. Python generates an initializer, so the class can be constructed without writing __init__ by hand.

from dataclasses import dataclass

@dataclass
class User:
    name: str
    active: bool = True

user = User("Ada")
print(user)
User(name='Ada', active=True)

:::

:::cell The generated instance still exposes ordinary attributes. A dataclass is a regular class with useful methods filled in.

print(user.name)
Ada

:::

:::cell Defaults can be overridden by keyword. The generated representation includes the field names, which is useful during debugging.

inactive = User("Guido", active=False)
print(inactive)
print(inactive.active)
User(name='Guido', active=False)
False

:::

:::note

  • Type annotations define dataclass fields.
  • Dataclasses generate methods but remain normal Python classes.
  • Use field() for advanced defaults such as per-instance lists or dictionaries. :::