<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Omri Grossman</title>
    <description>The latest articles on Forem by Omri Grossman (@omrigm).</description>
    <link>https://forem.com/omrigm</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F114571%2F47c7ecfc-b983-4b1c-bf99-46038dc1187b.jpeg</url>
      <title>Forem: Omri Grossman</title>
      <link>https://forem.com/omrigm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/omrigm"/>
    <language>en</language>
    <item>
      <title>5 Reasons Why Bite-Sized Functions Will Make You a Coding Hero</title>
      <dc:creator>Omri Grossman</dc:creator>
      <pubDate>Tue, 10 Sep 2024 21:01:30 +0000</pubDate>
      <link>https://forem.com/omrigm/5-reasons-why-bite-sized-functions-will-make-you-a-coding-hero-3hck</link>
      <guid>https://forem.com/omrigm/5-reasons-why-bite-sized-functions-will-make-you-a-coding-hero-3hck</guid>
      <description>&lt;p&gt;Hey there, fellow code enthusiasts! 👋 Ever found yourself lost in a sea of endless lines, wondering where one function ends and another begins? We've all been there. Today, let's talk about why breaking your code into smaller, manageable chunks isn't just a best practice – it's a game-changer for your development skills and career.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Your Future Self Will Thank You
&lt;/h2&gt;

&lt;p&gt;Picture this: It's 3 AM, you're knee-deep in a critical bug fix, and you encounter a function that spans hundreds of lines. Nightmare fuel, right? Smaller code chunks act like friendly signposts in your codebase. They make it infinitely easier to navigate, understand, and modify your code, even when you're running on caffeine and determination.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Instead of this:&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;doEverything&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// 200 lines of mixed responsibilities&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Aim for this:&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;validateInput&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* 20 lines */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;processData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* 30 lines */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;formatOutput&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* 25 lines */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Become the Teamwork MVP
&lt;/h2&gt;

&lt;p&gt;In the world of collaborative coding, being a team player is crucial. Smaller code chunks make your pull requests a joy to review. Your teammates can quickly understand your changes, provide meaningful feedback, and approve your work faster. Plus, you'll significantly reduce the chances of merge conflicts – a win-win for everyone involved!&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Level Up Your Testing Game
&lt;/h2&gt;

&lt;p&gt;Let's face it: testing is important, but it's not always the most exciting part of development. Smaller code chunks make unit testing not just easier, but sometimes even enjoyable (yes, really!). When each function has a clear, single responsibility, writing tests becomes more straightforward and your test coverage improves naturally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Easy to test:&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;calculateTotal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Nightmare to test:&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;processOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;coupon&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// 150 lines covering validation, calculation, database updates, and email sending&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Squash Bugs Like a Pro
&lt;/h2&gt;

&lt;p&gt;Bug hunting in a massive function is like finding a needle in a haystack. With smaller code chunks, you're essentially dividing that haystack into manageable piles. This makes debugging faster and more efficient. You can isolate issues more quickly and fix them with confidence, knowing you're less likely to introduce new bugs in the process.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Boost Your Code Reusability
&lt;/h2&gt;

&lt;p&gt;Smaller, focused code chunks are like LEGO blocks for your software. They're easier to pick up and use in different contexts. This not only makes your current project more flexible but also builds up a personal library of reliable code snippets you can carry from project to project. It's all about work smarter, not harder!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Highly reusable:&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;capitalizeString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Limited reusability:&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;formatUserData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// 100 lines of specific data manipulation&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Bonus Tip: A Tool to Keep You on Track
&lt;/h2&gt;

&lt;p&gt;Keeping your code chunks small and focused is a skill that develops over time. To help developers on this journey, I created a VS Code extension called Live Code Metrics. It provides real-time feedback on your function sizes, acting like a friendly code reviewer that's always by your side, gently nudging you towards better coding practices.&lt;/p&gt;

&lt;p&gt;Live Code Metrics offers features like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Real-time visualization of function sizes&lt;/li&gt;
&lt;li&gt;Customizable thresholds to match your team's standards&lt;/li&gt;
&lt;li&gt;Support for multiple languages including Java, JavaScript, TypeScript, React, and Python
&lt;a href="https://marketplace.visualstudio.com/items?itemName=OmriGrossman.live-code-metric" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6i6hy26wlnk5azxzztmh.png" alt="Live Code Metrics Logo" width="205" height="205"&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can easily find Live Code Metrics in two ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Check out the featured list in the VS Code marketplace&lt;/li&gt;
&lt;li&gt;Follow this link: &lt;a href="https://marketplace.visualstudio.com/items?itemName=OmriGrossman.live-code-metric" rel="noopener noreferrer"&gt;Live Code Metrics&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It's exciting to see the extension featured in the marketplace! If you're looking to build good habits and improve your code structure over time, give it a try. Remember, tools like these are meant to assist, not replace, your judgment. Use them as guides to develop your intuition for clean, maintainable code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Remember, the goal isn't to make every function tiny – it's about finding the right balance for readability and maintainability. Start by being mindful of your function sizes, and you'll naturally develop an instinct for when to break things down.&lt;/p&gt;

&lt;p&gt;So, next time you're coding, challenge yourself to keep it small and focused. Your code (and your fellow developers) will thank you for it!&lt;/p&gt;

&lt;p&gt;Happy coding, and may your functions be ever concise and clear! 💻✨&lt;/p&gt;




&lt;p&gt;What are your thoughts on managing code chunk sizes? Do you have any favorite techniques or tools? Share in the comments below – I'd love to hear your experiences!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Streamline Your Front-End Development with Tailwind Color Matcher for VSCode</title>
      <dc:creator>Omri Grossman</dc:creator>
      <pubDate>Mon, 11 Dec 2023 19:25:03 +0000</pubDate>
      <link>https://forem.com/omrigm/streamline-your-front-end-development-with-tailwind-color-matcher-for-vscode-4cld</link>
      <guid>https://forem.com/omrigm/streamline-your-front-end-development-with-tailwind-color-matcher-for-vscode-4cld</guid>
      <description>&lt;p&gt;As front-end developers, we are constantly looking for tools that not only enhance our workflow but also save precious time. That's where the "Tailwind Color Matcher" VSCode extension steps in - a game-changer for anyone working with Tailwind CSS.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://marketplace.visualstudio.com/items?itemName=OmriGrossman.tailwind-color-matcher"&gt;VSCode Marketplace download&lt;/a&gt;&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;&lt;a href="https://github.com/OmriGM/tailwind-color-matcher"&gt;Github Repository&lt;/a&gt;&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is Tailwind Color Matcher?
&lt;/h3&gt;

&lt;p&gt;"Tailwind Color Matcher" is an innovative extension for Visual Studio Code that makes it incredibly easy to convert HEX color values to their nearest Tailwind CSS class equivalent.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XQk5liF6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzgvh2yrhoxveojwgnmj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XQk5liF6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzgvh2yrhoxveojwgnmj.gif" alt="Tailwind color matcher screenshot demo" width="652" height="412"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How Does It Save Time?
&lt;/h3&gt;

&lt;p&gt;Gone are the days of manually searching for color equivalents or flipping back and forth between documentation and your code editor. With this extension, you can:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Instantly Translate Colors:&lt;/strong&gt; Simply input a HEX color value in VSCode's command palette, and the extension suggests the nearest Tailwind class.&lt;br&gt;
&lt;strong&gt;Use the Color Picker:&lt;/strong&gt; Not sure about the HEX value? Use the built-in color picker for real-time suggestions.&lt;/p&gt;

&lt;h3&gt;
  
  
  A User Experience That Stands Out
&lt;/h3&gt;

&lt;p&gt;What makes "Tailwind Color Matcher" stand out is its focus on a seamless user experience. The intuitive interface allows you to:&lt;/p&gt;

&lt;h4&gt;
  
  
  Easily navigate and use features without any learning curve.
&lt;/h4&gt;

&lt;p&gt;View color previews as you select, ensuring you're always confident in your choices.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9EtZrETU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nn342egx314qocqkyxk9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9EtZrETU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nn342egx314qocqkyxk9.gif" alt="color picker" width="600" height="790"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Save Colors for Later Use
&lt;/h4&gt;

&lt;p&gt;A unique feature of this extension is its ability to save your chosen colors. This means you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maintain a consistent color scheme throughout your project.&lt;/li&gt;
&lt;li&gt;Quickly access frequently used colors with a quick copy action, streamlining your workflow even further.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mq0rm0Vm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0rutls04leawmayscdu4.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mq0rm0Vm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0rutls04leawmayscdu4.gif" alt="color saving" width="600" height="1133"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  In Conclusion
&lt;/h3&gt;

&lt;p&gt;Whether you're a seasoned front-end developer or just starting with Tailwind CSS, "Tailwind Color Matcher" is designed to make your development process smoother, faster, and more enjoyable. Try it out today and experience a new level of efficiency in your coding journey!&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>frontend</category>
      <category>tailwindcss</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Why You Should Use Python Data Classes</title>
      <dc:creator>Omri Grossman</dc:creator>
      <pubDate>Sun, 02 Jan 2022 14:01:44 +0000</pubDate>
      <link>https://forem.com/omrigm/why-you-should-use-python-data-classes-48po</link>
      <guid>https://forem.com/omrigm/why-you-should-use-python-data-classes-48po</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;If you just started or already coded using Python and like Object Oriented Programming but aren't familiar with the &lt;code&gt;dataclasses&lt;/code&gt; module, you came to the right place!&lt;/p&gt;

&lt;h3&gt;
  
  
  In this article, we will learn:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;What are data classes, and what are their benefits.&lt;/li&gt;
&lt;li&gt;How exactly they are different from regular Python classes.&lt;/li&gt;
&lt;li&gt;And when you should use them.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Data Classes Background
&lt;/h2&gt;

&lt;p&gt;Data classes are used mainly to model data in Python. It decorates regular Python classes and has no restrictions, which means it can behave like a typical class.&lt;/p&gt;

&lt;p&gt;A small example of a Data Class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;dataclasses&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dataclass&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;dataclass&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
   &lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
   &lt;span class="n"&gt;top_speed_km&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  &lt;code&gt;dataclasses&lt;/code&gt; was introduced in Python 3.7 as part of PEP 557
&lt;/h6&gt;

&lt;h4&gt;
  
  
  Let's dive into some code examples
&lt;/h4&gt;




&lt;h2&gt;
  
  
  The Benefits of Data Class
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Special methods build-in implementation
&lt;/h4&gt;

&lt;p&gt;When using the @dataclass decorator we don't have to implement   &lt;a href="https://docs.python.org/3/glossary.html#term-special-method"&gt;special methods&lt;/a&gt;  ourselves, which helps us &lt;strong&gt;avoid boilerplate code&lt;/strong&gt;, like the init method (_&lt;em&gt;init&lt;/em&gt;_ ), string representation method (_&lt;em&gt;repr&lt;/em&gt;_ ), methods that are used for ordering objects (e.g. &lt;strong&gt;lt&lt;/strong&gt;, &lt;strong&gt;le&lt;/strong&gt;, &lt;strong&gt;gt&lt;/strong&gt;, and &lt;strong&gt;ge&lt;/strong&gt;), &lt;strong&gt;these compare the class as if it were a tuple of its fields&lt;/strong&gt;, in order. &lt;br&gt;
Read about a few other extra built-in methods in the &lt;a href="https://docs.python.org/3/library/dataclasses.html#module-contents"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;How will it look with a regular class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
  &lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
  &lt;span class="n"&gt;top_speed_km&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt;  &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;top_speed_km&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manufacturer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;manufacturer&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;top_speed_km&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;top_speed_km&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__lt__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;other_car&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;top_speed_km&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;other_car&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;top_speed_km&lt;/span&gt;

&lt;span class="n"&gt;red_ferrari&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'red'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Ferrari'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;top_speed_km&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;320&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;red_ferrari&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# &amp;lt;__main__.Car object at 0x7f218789ca00&amp;gt;
&lt;/span&gt;&lt;span class="n"&gt;black_ferrari&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'red'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Ferrari'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;top_speed_km&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;347&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;red_ferrari&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;black_ferrari&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note those two points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Because we didn't implement the _&lt;em&gt;repr&lt;/em&gt;_ special method, when we print the Car instance, we get the name of the class and the object address.&lt;/li&gt;
&lt;li&gt;To compare between 2 Car instances, I had to implement the "less than" (_&lt;em&gt;lt&lt;/em&gt;_) method by myself. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example with dataclass decorator&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;dataclasses&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dataclass&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;dataclass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
  &lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
  &lt;span class="n"&gt;top_speed_km&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;

&lt;span class="n"&gt;slow_tesla&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;top_speed_km&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;261&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'white'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Tesla'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow_tesla&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# Car(color='white', manufacturer='Tesla', top_speed_km=261)
&lt;/span&gt;&lt;span class="n"&gt;fast_tesla&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;top_speed_km&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;280&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'white'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Tesla'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slow_tesla&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;fast_tesla&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# True
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  It's necessary to set &lt;code&gt;order=True&lt;/code&gt; if we want special order methods implementation to be included in the dataclass (e.g. &lt;strong&gt;lt&lt;/strong&gt;)
&lt;/h6&gt;

&lt;ul&gt;
&lt;li&gt;When we try to print the &lt;code&gt;slow_tesla&lt;/code&gt; object, we see the actual values of the object, not the object's address, unlike the previous example.&lt;/li&gt;
&lt;li&gt;We can compare two objects without any need for us to implement special methods.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Inheritance
&lt;/h4&gt;

&lt;p&gt;Same as regular python classes, inheritance can come to our advantage here too, no need to deal with the parent class construction:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;dataclasses&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dataclass&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;dataclass&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
  &lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
  &lt;span class="n"&gt;top_speed_km&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;dataclass&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ElectricCar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;battery_capacity_kwh&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;
  &lt;span class="n"&gt;maximum_range_km&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;

&lt;span class="n"&gt;white_tesla_model_3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ElectricCar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'white'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Tesla'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;top_speed_km&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;261&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;battery_capacity_kwh&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;maximum_range_km&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;455&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;white_tesla_model_3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# ElectricCar(color='white', manufacturer='Tesla', top_speed_km=261, battery_capacity_kwh=50, maximum_range_km=455)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just for reference, here is how it will look like using a regular class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
  &lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
  &lt;span class="n"&gt;top_speed_km&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt;  &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;top_speed_km&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manufacturer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;manufacturer&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;top_speed_km&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;top_speed_km&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ElectricCar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;battery_capacity_kwh&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;
  &lt;span class="n"&gt;maximum_range_km&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;top_speed_km&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;battery_capacity_kwh&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;maximum_range_km&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
      &lt;span class="nb"&gt;super&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;top_speed_km&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;battery_capacity_kwh&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;battery_capacity_kwh&lt;/span&gt;
      &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;maximum_range_km&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;maximum_range_km&lt;/span&gt;

&lt;span class="n"&gt;white_tesla_model_3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ElectricCar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'white'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Tesla'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;top_speed_km&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;261&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;battery_capacity_kwh&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;maximum_range_km&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;455&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;white_tesla_model_3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope you can see that we saved much boilerplate code even in this small code snippet and didn't repeat every parameter initiation.&lt;/p&gt;

&lt;h4&gt;
  
  
  Frozen Instances
&lt;/h4&gt;

&lt;p&gt;By passing &lt;code&gt;frozen=True&lt;/code&gt; to the data class decorator, it lets us create immutable Python objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;dataclasses&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dataclass&lt;/span&gt;

&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;dataclass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frozen&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
  &lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;
  &lt;span class="n"&gt;top_speed_km&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;


&lt;span class="n"&gt;white_tesla&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'white'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;manufacturer&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Tesla'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;top_speed_km&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;261&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;white_tesla&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'Red'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Trying to modify &lt;code&gt;white_tesla&lt;/code&gt; to a red tesla, will give us a &lt;strong&gt;FrozenInstanceError&lt;/strong&gt; error message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dataclasses.FrozenInstanceError: cannot assign to field 'color'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  Note: Using Frozen Instances will hurt the performance a bit so use it carefully
&lt;/h6&gt;




&lt;h3&gt;
  
  
  Now, you:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Are familiar with what data class is and how to use it.&lt;/li&gt;
&lt;li&gt;Learned about the benefits and use cases of data classes.&lt;/li&gt;
&lt;li&gt;Have some code examples which can help you get started.&lt;/li&gt;
&lt;li&gt;Can start using it in your projects.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;dataclasses&lt;/code&gt; is a powerful module that helps us, Python developers, model our data, avoid writing boilerplate code, and write much cleaner and elegant code.&lt;br&gt;
I encourage you to explore and learn more about data class special features, I use it in all of my projects, and I recommend you to do it too.&lt;/p&gt;

&lt;h3&gt;
  
  
  Extra Resources
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://docs.python.org/3/library/dataclasses.html"&gt;https://docs.python.org/3/library/dataclasses.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://realpython.com/python-data-classes/"&gt;https://realpython.com/python-data-classes/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>programming</category>
      <category>productivity</category>
    </item>
    <item>
      <title>Run Machine Learning models in your browser with TensorFlow.js (ReactJS)</title>
      <dc:creator>Omri Grossman</dc:creator>
      <pubDate>Wed, 28 Jul 2021 14:59:51 +0000</pubDate>
      <link>https://forem.com/omrigm/run-machine-learning-models-in-your-browser-with-tensorflow-js-reactjs-48pe</link>
      <guid>https://forem.com/omrigm/run-machine-learning-models-in-your-browser-with-tensorflow-js-reactjs-48pe</guid>
      <description>&lt;p&gt;&lt;a href="https://www.tensorflow.org/js" rel="noopener noreferrer"&gt;TensorFlow.js&lt;/a&gt; (or, in short, tfjs) is a library that lets you create, train, and use trained Machine Learning models in Javascript!&lt;br&gt;
The main focus is to let Javascript Developers enter the Machine Learning &amp;amp; Deep Learning world by creating cool and intelligent web applications that can run on any major browser or &lt;a href="https://nodejs.org/en/about/" rel="noopener noreferrer"&gt;Node.js&lt;/a&gt; servers using Javascript.&lt;/p&gt;
&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;TensorFlow.js can run almost everywhere, all major browsers, servers, mobile phones, and even IoT devices. This demonstrates how huge potential this library has. TensorFlow.js backend can run on the device GPU through &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API" rel="noopener noreferrer"&gt;WebGLAPI&lt;/a&gt;, which gives Javascript code to run on the GPU, which means that TensorFlow.js can have excellent performance even though it runs on the browser.&lt;/p&gt;
&lt;h3&gt;
  
  
  After reading this post, you will:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Learn about TensorFlow.js and the ways you can use it.&lt;/li&gt;
&lt;li&gt;Know how to load Machine Learning models into your Javascript project and start using it.&lt;/li&gt;
&lt;li&gt;Gain the skills to create such a project by yourself&lt;/li&gt;
&lt;li&gt;And finally, gain more knowledge about Machine Learning.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  So, how does it work?
&lt;/h3&gt;

&lt;p&gt;There are several options that we can choose from:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyjxmjda7vtlpfh4po07f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyjxmjda7vtlpfh4po07f.png" alt="tensorflowjs-models"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  1. Run existing models:
&lt;/h3&gt;

&lt;p&gt;TensorFlow.js provided us few attractive pre-trained models that we can import into our project, provide input, and use the output to our requirements, here you can explore the models they are providing for us: &lt;a href="https://www.tensorflow.org/js/models" rel="noopener noreferrer"&gt;TensorFlow.js Models&lt;/a&gt;, and they keep adding more models as time goes by.&lt;br&gt;
In addition to that, you can find many attractive pre-trained models developed by the TensorFlow.js community all across the web.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Retrain existing models:
&lt;/h3&gt;

&lt;p&gt;This option allows us to improve an existing model for our specific use-case. We can achieve that by using a method called: Transfer Learning.&lt;br&gt;
Transfer learning is the improvement of learning in a new task by transferring knowledge from a related task that has already been learned.&lt;br&gt;
For instance, in the real-world, the balancing logic learned while riding a bicycle can be transferred to learn driving other two-wheeled vehicles. Similarly, in machine learning, transfer learning can be used to transfer the algorithmic logic from one ML model to the other.&lt;/p&gt;
&lt;h3&gt;
  
  
  3. Develop ML with JavaScript:
&lt;/h3&gt;

&lt;p&gt;The third option will be used for situations where the developer wants to create a new Machine Learning model from scratch, using TensorFlow.js API, just like the regular TensorFlow version.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now let’s get our hands dirty and do some Machine Learning with Javascript&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this article, our primary focus will be adding and running a pre-trained Machine Learning model to a Javascript project. You will see how easy it is to install, load, and run predictions on the machine learning model.&lt;/p&gt;
&lt;h4&gt;
  
  
  So let’s get started!
&lt;/h4&gt;

&lt;p&gt;I've built an application that demonstrates the use of a pre-trained image tag classification model created by the Tensorflow.js team. The model is called MobileNet, and you can find more information about it &lt;a href="https://github.com/tensorflow/tfjs-models/tree/master/mobilenet" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/ch2g5"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;The demo application is built with &lt;a href="https://reactjs.org/" rel="noopener noreferrer"&gt;React.js&lt;/a&gt;, and &lt;a href="https://ant.design/docs/react/introduce" rel="noopener noreferrer"&gt;Ant Design&lt;/a&gt; for the UI components.&lt;/p&gt;

&lt;p&gt;React is an open-source, front end, JavaScript library for building user interfaces or UI components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s walk through the main parts of the application together:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, dependencies&lt;/p&gt;

&lt;p&gt;After we set up our React application, we will need to install tfjs and the image classification model — mobilenet, by running the commands below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm i @tensorflow-models/mobilenet
$ npm i @tensorflow/tfjs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Now, after we installed the packages, we can import them to our &lt;code&gt;App.js&lt;/code&gt; file:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;We imported the image classification model and the TensorFlow.js engine, which runs the machine learning models in the background every time we invoke the model.&lt;/p&gt;

&lt;p&gt;Next up, we need to load the model into our component for future use. Please note that the model.load() function is an asynchronous function, so we need to wait for it to be completed.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The mobileNet model has a method called classify, after we loaded the model we can call this method：&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This method accepts 2 arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;img&lt;/strong&gt;: A Tensor or an image element to make a classification on.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;topk&lt;/strong&gt;: How many of the top probabilities to return. Defaults to 3.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the next step we want to read the user input image and load the uploaded file into a canvas element of type HTMLCanvasElement&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;After the image is loaded into the canvas we can run the classification method.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The output of the model.classify method is an array of classified labels and their prediction score. The output looks like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Once we saved the predictions array in our component we can loop over the array and render them to the screen:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;&lt;em&gt;So that’s it, we have a living Machine Learning model in our browser, congratulations!&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  You're welcomed visit those links for:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The full code of the application can be found in &lt;a href="https://bit.ly/3BPVATz" rel="noopener noreferrer"&gt;this repository&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Full code + live demo: &lt;a href="http://bit.ly/2KYKbuD" rel="noopener noreferrer"&gt;TensorFlow.js Image Classification&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can upload your own images, get predictions, and can even be more creative and try to add new features by yourself!&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;There is no doubt that the use of machine learning is continuously increasing. With Javascript development becoming even more popular, the TensorFlow.js community will grow and get more powerful. I think we will see more and more production-grade applications running TensorFlow.js in the browser or Node.js servers for simple, light-weight tasks that Machine Learning models can solve.&lt;/p&gt;

&lt;p&gt;After you’ve all seen how fast and easy it is to integrate TensorFlow.js to a Javascript application, I invite you all to try it by yourself and create some cool projects and share them with community.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>machinelearning</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
