<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/dataclasses-for-structured-application-data-eemptk7qp" -->

---
title: Dataclasses for Structured Application Data | daily.dev
description: Python's dataclass decorator offers a stdlib alternative to fragile configuration dictionaries, generating __init__, __repr__, and __eq__ automatically while...
canonical: https://daily.dev/posts/dataclasses-for-structured-application-data-eemptk7qp
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Dataclasses for Structured Application Data | daily.dev
og:description: Python's dataclass decorator offers a stdlib alternative to fragile configuration dictionaries, generating __init__, __repr__, and __eq__ automatically while...
og:url: https://daily.dev/posts/dataclasses-for-structured-application-data-eemptk7qp
og:image: https://api.daily.dev/og/posts/EEmPtk7QP.png
og:image:alt: Dataclasses for Structured Application Data
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Dataclasses for Structured Application Data

**[Machine Learning Mastery](https://daily.dev/sources/mlm)** · 9 min read · 0 upvotes · 0 comments

## Summary

Python's dataclass decorator offers a stdlib alternative to fragile configuration dictionaries, generating __init__, __repr__, and __eq__ automatically while leaving type annotations unenforced at runtime. Covers composing nested dataclasses, handling mutable defaults with default_factory, enforcing invariants in __post_init__, freezing configuration snapshots with frozen=True, and the manual work required to serialize/deserialize at JSON boundaries with asdict() and explicit reconstruction. Closes with guidance on when to use a plain dict, a dataclass, or Pydantic depending on how much the data is trusted and owned.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://machinelearningmastery.com/dataclasses-for-structured-application-data>

## Questions this post answers

### How do I avoid the shared mutable default list bug when using Python dataclasses?

Assigning a mutable default like list[str] = [] directly in a dataclass field raises a ValueError at class-definition time, since Python refuses to let all instances share one mutable object. The fix is field(default_factory=list), which builds a fresh list for every instance at construction time, and the same pattern applies to nested dataclass defaults like RetryPolicy.

_Developers tightening up config defaults can find similar Python gotchas curated on daily.dev._

### Do Python dataclass type annotations get validated at runtime?

No, dataclass field annotations are not checked at runtime. Per PEP 557, the decorator only uses annotations to discover fields; a call like JobConfig("nightly-import", batch_size="lots") constructs without error even though batch_size is typed as int, and the invalid value only fails later when arithmetic is attempted on it.

_Anyone deciding between dataclasses and stricter validation can track these tradeoffs on daily.dev._

### When should I use Pydantic instead of a plain Python dataclass?

Pydantic is the better choice when data crosses in from somewhere you don't control, such as user input, an external API response, or a manually edited config file, because it offers coercion and detailed multi-field validation errors that a dataclass's __post_init__ hook shouldn't try to replicate. Dataclasses fit trusted, application-owned data instead.

_Teams weighing dataclasses against Pydantic can follow both ecosystems on daily.dev._

## Similar posts on daily.dev

- [Making friendly classes](https://daily.dev/posts/making-friendly-classes-jjijveqdg) · Planet Python · 0 upvotes · 0 comments
- [Learning Python in Public, Part 2: What Finally Clicked About Classes](https://daily.dev/posts/learning-python-in-public-part-2-what-finally-clicked-about-classes-g3yfbio0s) · Medium · 0 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python), [#pydantic](https://daily.dev/tags/pydantic)

[View this post on daily.dev](https://daily.dev/posts/dataclasses-for-structured-application-data-eemptk7qp)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Dataclasses for Structured Application Data","url":"https://daily.dev/posts/dataclasses-for-structured-application-data-eemptk7qp","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/dataclasses-for-structured-application-data-eemptk7qp"},"datePublished":"2026-09-04T12:44:08.960Z","dateModified":"2026-09-14T08:23:40.139Z","description":"Python's dataclass decorator offers a stdlib alternative to fragile configuration dictionaries, generating __init__, __repr__, and __eq__ automatically while...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/eb44210f30964d3b4933b2629a651010?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/eb44210f30964d3b4933b2629a651010?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Machine Learning Mastery","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Machine Learning Mastery","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/795781ffb32446c39eba040feb64f505","url":"https://daily.dev/sources/mlm"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/dataclasses-for-structured-application-data-eemptk7qp","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python,pydantic","timeRequired":"PT9M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Machine Learning Mastery","item":"https://daily.dev/sources/mlm"},{"@type":"ListItem","position":3,"name":"Dataclasses for Structured Application Data"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/dataclasses-for-structured-application-data-eemptk7qp#faq","mainEntity":[{"@type":"Question","name":"How do I avoid the shared mutable default list bug when using Python dataclasses?","acceptedAnswer":{"@type":"Answer","text":"Assigning a mutable default like list[str] = [] directly in a dataclass field raises a ValueError at class-definition time, since Python refuses to let all instances share one mutable object. The fix is field(default_factory=list), which builds a fresh list for every instance at construction time, and the same pattern applies to nested dataclass defaults like RetryPolicy. Developers tightening up config defaults can find similar Python gotchas curated on daily.dev."}},{"@type":"Question","name":"Do Python dataclass type annotations get validated at runtime?","acceptedAnswer":{"@type":"Answer","text":"No, dataclass field annotations are not checked at runtime. Per PEP 557, the decorator only uses annotations to discover fields; a call like JobConfig(\"nightly-import\", batch_size=\"lots\") constructs without error even though batch_size is typed as int, and the invalid value only fails later when arithmetic is attempted on it. Anyone deciding between dataclasses and stricter validation can track these tradeoffs on daily.dev."}},{"@type":"Question","name":"When should I use Pydantic instead of a plain Python dataclass?","acceptedAnswer":{"@type":"Answer","text":"Pydantic is the better choice when data crosses in from somewhere you don't control, such as user input, an external API response, or a manually edited config file, because it offers coercion and detailed multi-field validation errors that a dataclass's __post_init__ hook shouldn't try to replicate. Dataclasses fit trusted, application-owned data instead. Teams weighing dataclasses against Pydantic can follow both ecosystems on daily.dev."}}]}
```

