<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>DataClass &amp;mdash; sandeepk</title>
    <link>https://blogs.dgplug.org/sandeepk/tag:DataClass</link>
    <description></description>
    <pubDate>Sun, 19 Apr 2026 06:16:09 +0000</pubDate>
    <item>
      <title>Python : Data Class</title>
      <link>https://blogs.dgplug.org/sandeepk/python-data-class</link>
      <description>&lt;![CDATA[A data class is a class containing data only, from Python3.7 we can define a data class with the help of decorator @dataclass, which build the class with the basic functionality like init , repr, eq and more special methods.&#xA;&#xA;Let see how to define your data class with the decorator @dataclass&#xA;from dataclasses import dataclass&#xA;&#xA;@dataclass&#xA;class Batch:&#xA;    sku: int&#xA;    name: str&#xA;    qty: int&#xA;&#xA;      Batch(1, &#39;desk&#39;, 100)&#xA;Batch(sku=1, name=&#39;desk&#39;, qty=100)&#xA;We can also add the default value to the data class, which works exactly as if we do in the init  method of regular class. As you have noticed in the above we have defined the fields with type hints, which is kind of mandatory thing in the data class, if you do not do it will not take the field in your data class.&#xA;&#xA;@dataclass&#xA;class Batch:&#xA;    sku: int = 1&#xA;    name: str = &#39;desk&#39;&#xA;    qty: int = 100&#xA;&#xA;if you don&#39;t want to explicity type the fields you can use any&#xA;from typing import Any&#xA;class AnyBatch:&#xA;    sku: Any&#xA;    name: Any = &#39;desk&#39;&#xA;    qty: Any = 100&#xA;&#xA;If you want to define mutable default value in data class, it can be done with the help of defaultfactory and field. Field() is used to customize each field in data class, different parameter that can be passed to field are defaultfactory, compare, hash, init, you can check about them over here &#xA;from dataclasses import dataclass, field&#xA;from typing import List&#xA;&#xA;@dataclass()&#xA;class Batch:&#xA;    sku: int&#xA;    name: str&#xA;    qty: int = 0&#xA;    creator: List[str] = field(default_factory=function/mutable value)&#xA;&#xA;Immutable Data Class&#xA;we can also define our data class as immutable by setting frozen=True, which basically means we cannot assign value to the fields after creation&#xA;@dataclass(frozen=True)&#xA;class Batch:&#xA;    sku: int&#xA;    name: str&#xA;    qty: int = 0&#xA;&#xA;      b = Batch(12, &#39;desk&#39;, 100)&#xA;      b.qty &#xA;100&#xA;      b.qty = 90&#xA;dataclasses.FrozenInstanceError: cannot assign to field &#39;qty&#39;&#xA;Data class saves us from writing boilerplate code, help us to focus on logic, this new feature of Python3.7 is great, so what waiting for go and right some data classes.&#xA;&#xA;Cheers!&#xA;&#xA;100DaysToOffload&#xA;Python&#xA;DataClass&#xA;TIL&#xA;DGPLUG&#xA;]]&gt;</description>
      <content:encoded><![CDATA[<p>A data class is a class containing data only, from Python3.7 we can define a data class with the help of decorator <code>@dataclass</code>, which build the class with the basic functionality like <code>__init__</code> , <code>__repr__</code>, <code>__eq__</code> and more special methods.</p>

<p>Let see how to define your data class with the decorator <code>@dataclass</code></p>

<pre><code class="language-python">from dataclasses import dataclass

@dataclass
class Batch:
    sku: int
    name: str
    qty: int

&gt;&gt;&gt; Batch(1, &#39;desk&#39;, 100)
Batch(sku=1, name=&#39;desk&#39;, qty=100)
</code></pre>

<p>We can also add the default value to the data class, which works exactly as if we do in the <code>__init__</code>  method of regular class. As you have noticed in the above we have defined the fields with type hints, which is kind of mandatory thing in the data class, if you do not do it will not take the field in your data class.</p>

<pre><code class="language-python">@dataclass
class Batch:
    sku: int = 1
    name: str = &#39;desk&#39;
    qty: int = 100

# if you don&#39;t want to explicity type the fields you can use any
from typing import Any
class AnyBatch:
    sku: Any
    name: Any = &#39;desk&#39;
    qty: Any = 100

</code></pre>

<p>If you want to define mutable default value in data class, it can be done with the help of <code>default_factory</code> and <code>field</code>. Field() is used to customize each field in data class, different parameter that can be passed to field are <code>default_factory</code>, <code>compare</code>, <code>hash</code>, <code>init</code>, you can check about them over <a href="https://docs.python.org/3/library/dataclasses.html#dataclasses.field" rel="nofollow">here </a></p>

<pre><code class="language-python">from dataclasses import dataclass, field
from typing import List

@dataclass()
class Batch:
    sku: int
    name: str
    qty: int = 0
    creator: List[str] = field(default_factory=&lt;function/mutable value&gt;)
</code></pre>

<p><strong>Immutable Data Class</strong>
we can also define our data class as immutable by setting <code>frozen=True</code>, which basically means we cannot assign value to the fields after creation</p>

<pre><code class="language-python">@dataclass(frozen=True)
class Batch:
    sku: int
    name: str
    qty: int = 0

&gt;&gt;&gt; b = Batch(12, &#39;desk&#39;, 100)
&gt;&gt;&gt; b.qty 
100
&gt;&gt;&gt; b.qty = 90
dataclasses.FrozenInstanceError: cannot assign to field &#39;qty&#39;
</code></pre>

<p>Data class saves us from writing boilerplate code, help us to focus on logic, this new feature of Python3.7 is great, so what waiting for go and right some data classes.</p>

<p>Cheers!</p>

<p><a href="/sandeepk/tag:100DaysToOffload" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">100DaysToOffload</span></a>
<a href="/sandeepk/tag:Python" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Python</span></a>
<a href="/sandeepk/tag:DataClass" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">DataClass</span></a>
<a href="/sandeepk/tag:TIL" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">TIL</span></a>
<a href="/sandeepk/tag:DGPLUG" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">DGPLUG</span></a></p>
]]></content:encoded>
      <guid>https://blogs.dgplug.org/sandeepk/python-data-class</guid>
      <pubDate>Fri, 03 Sep 2021 12:54:52 +0000</pubDate>
    </item>
  </channel>
</rss>