{"id":116,"date":"2026-03-02T08:53:30","date_gmt":"2026-03-02T03:53:30","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=116"},"modified":"2026-03-16T10:03:22","modified_gmt":"2026-03-16T05:03:22","slug":"dataclasses","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/dataclasses\/","title":{"rendered":"\u00a0Dataclasses"},"content":{"rendered":"\n<p>Dataclasses are a special feature in Python that help you create classes used mainly for storing data.<\/p>\n\n\n\n<p>They reduce boilerplate code by automatically generating common methods like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>__init__()<\/code><\/li>\n\n\n\n<li><code>__repr__()<\/code><\/li>\n\n\n\n<li><code>__eq__()<\/code><\/li>\n<\/ul>\n\n\n\n<p>Dataclasses were introduced in Python 3.7.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Why Use Dataclasses?<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Less code<\/li>\n\n\n\n<li>Cleaner syntax<\/li>\n\n\n\n<li>Automatic constructor creation<\/li>\n\n\n\n<li>Built-in comparison support<\/li>\n\n\n\n<li>Easy to read and maintain<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating a Dataclass<\/strong><\/h2>\n\n\n\n<p>To use dataclasses, import the <code>dataclass<\/code> decorator from the <code>dataclasses<\/code> module.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from dataclasses import dataclass@dataclass<br>class Student:<br>    name: str<br>    age: int<br>    marks: floats1 = Student(\"Hira\", 22, 85.5)<br>print(s1)<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Student(name='Hira', age=22, marks=85.5)<\/pre>\n\n\n\n<p>Notice:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>No need to write <code>__init__()<\/code><\/li>\n\n\n\n<li>No need to write <code>__str__()<\/code><\/li>\n\n\n\n<li>Everything is automatically created<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Equality Comparison<\/strong><\/h2>\n\n\n\n<p>Dataclasses automatically support comparison.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">s1 = Student(\"Hira\", 22, 85.5)<br>s2 = Student(\"Hira\", 22, 85.5)print(s1 == s2)<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">True<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Default Values<\/strong><\/h2>\n\n\n\n<p>You can provide default values.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from dataclasses import dataclass@dataclass<br>class Student:<br>    name: str<br>    age: int = 18<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Using field() for Advanced Options<\/strong><\/h2>\n\n\n\n<p>The <code>field()<\/code> function allows customization.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from dataclasses import dataclass, field@dataclass<br>class Student:<br>    name: str<br>    age: int<br>    marks: float = field(default=0.0)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Frozen Dataclass (Immutable)<\/strong><\/h2>\n\n\n\n<p>To make objects immutable:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from dataclasses import dataclass@dataclass(frozen=True)<br>class Student:<br>    name: str<br>    age: ints1 = Student(\"Hira\", 22)<\/pre>\n\n\n\n<p>Now you cannot modify attributes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Dataclass vs Normal Class<\/strong><\/h2>\n\n\n\n<p>Normal Class<br>You manually write <code>__init__()<\/code><br>More code<br>Manual comparison methods<\/p>\n\n\n\n<p>Dataclass<br>Auto-generated methods<br>Less code<br>Built-in comparison<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>When to Use Dataclasses<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When class is mainly used to store data<\/li>\n\n\n\n<li>When you want clean and readable code<\/li>\n\n\n\n<li>When you need automatic comparison<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Key Takeaway<\/strong><\/h2>\n\n\n\n<p>Dataclasses simplify class creation for data storage.<\/p>\n\n\n\n<p>They automatically generate important methods and make Python code cleaner, shorter, and easier to maintain.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > Advanced OOP > Dataclasses<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773637163402\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":58,"template":"","class_list":["post-116","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>\u00a0Dataclasses - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Python dataclasses and the @dataclass decorator to create clean classes for storing data with less boilerplate code.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/dataclasses\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u00a0Dataclasses - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Python dataclasses and the @dataclass decorator to create clean classes for storing data with less boilerplate code.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/dataclasses\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-16T05:03:22+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/dataclasses\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/dataclasses\\\/\",\"name\":\"\u00a0Dataclasses - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-02T03:53:30+00:00\",\"dateModified\":\"2026-03-16T05:03:22+00:00\",\"description\":\"Learn Python dataclasses and the @dataclass decorator to create clean classes for storing data with less boilerplate code.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/dataclasses\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/dataclasses\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/dataclasses\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > Advanced OOP > Dataclasses\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u00a0Dataclasses - One Language. Endless Possibilities","description":"Learn Python dataclasses and the @dataclass decorator to create clean classes for storing data with less boilerplate code.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/python\/lesson\/dataclasses\/","og_locale":"en_US","og_type":"article","og_title":"\u00a0Dataclasses - One Language. Endless Possibilities","og_description":"Learn Python dataclasses and the @dataclass decorator to create clean classes for storing data with less boilerplate code.","og_url":"https:\/\/gigz.pk\/python\/lesson\/dataclasses\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-16T05:03:22+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/dataclasses\/","url":"https:\/\/gigz.pk\/python\/lesson\/dataclasses\/","name":"\u00a0Dataclasses - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-02T03:53:30+00:00","dateModified":"2026-03-16T05:03:22+00:00","description":"Learn Python dataclasses and the @dataclass decorator to create clean classes for storing data with less boilerplate code.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/dataclasses\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/dataclasses\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/dataclasses\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"OBJECT ORIENTED PROGRAMMING IN PYTHON (OOP) > Advanced OOP > Dataclasses"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}