<?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>DEV Community: Shameel Uddin</title>
    <description>The latest articles on DEV Community by Shameel Uddin (@shameel).</description>
    <link>https://dev.to/shameel</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F859181%2F6d37469a-a032-4802-8a74-9cb9dfd55ce3.jpg</url>
      <title>DEV Community: Shameel Uddin</title>
      <link>https://dev.to/shameel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shameel"/>
    <language>en</language>
    <item>
      <title>What are Class Variables in Python OOP?</title>
      <dc:creator>Shameel Uddin</dc:creator>
      <pubDate>Sat, 20 Jun 2026 02:48:36 +0000</pubDate>
      <link>https://dev.to/shameel/what-are-class-variables-in-python-oop-2aag</link>
      <guid>https://dev.to/shameel/what-are-class-variables-in-python-oop-2aag</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In Python, class variables (also known as class attributes) &lt;strong&gt;are shared across all instances (objects) of a class.&lt;/strong&gt; They belong to the class itself, not to any specific instance.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problem: Why do we need Class Variables?
&lt;/h3&gt;

&lt;p&gt;Consider the following code snippet:&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="c1"&gt;#We have a class "InefficientUser" defined with some basic data
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;InefficientUser&lt;/span&gt;&lt;span class="p"&gt;:&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="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;    
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;        
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;max_login_attempts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; 
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;password_min_length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;

&lt;span class="c1"&gt;# Creating two different user instances from this class:
&lt;/span&gt;
&lt;span class="n"&gt;u1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;InefficientUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Shameel&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;u2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;InefficientUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Ammad&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this class, the instance variables &lt;code&gt;max_login_attempts&lt;/code&gt; and &lt;code&gt;password_min_length&lt;/code&gt; are the values which are common and needs to be shared across all instances, whereas the username has to be a unique value for all instances.&lt;/p&gt;

&lt;p&gt;Now, suppose for some reason, I want to change the value of &lt;code&gt;max_login_attempts&lt;/code&gt; from 8 to 10 for the whole system. In such a case, I will have to manually change the value of &lt;code&gt;max_login_attempts&lt;/code&gt; to 10 for every single instance, like this:&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="n"&gt;u1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;max_login_attempts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="n"&gt;u2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;max_login_attempts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The aforementioned method of modifying values might for work less number of instances, but consider your code having 100 instances from a class, and manually changing the value of a common variable in all of them? That would be a headache! So this method is Tedious, time-consuming, and error-prone as well.&lt;/p&gt;

&lt;p&gt;This is where the &lt;strong&gt;"Class Variables"&lt;/strong&gt; kick in with their magic! &lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution: Class Variables to the rescue!
&lt;/h2&gt;

&lt;p&gt;Class variables (also called class attributes) are variables that are defined inside a class but outside any instance methods or the &lt;code&gt;__init__&lt;/code&gt;constructor. They hold data that is shared universally by all instances of that class, rather than data unique to each individual object. You may imagine them as global or shared data variables.&lt;/p&gt;

&lt;p&gt;Consider the following code snippet:&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="c1"&gt;# We have a class "User" defined with some basic data. But this time, some variables are defined outside the __init__ method. These variables are "Class Variables".
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Class Variables 
&lt;/span&gt;    &lt;span class="n"&gt;max_login_attempts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; 
    &lt;span class="n"&gt;password_min_length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;
    &lt;span class="n"&gt;system_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hasabTech Portal&lt;/span&gt;&lt;span class="sh"&gt;"&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="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;  
        &lt;span class="c1"&gt;# Instance Variable  
&lt;/span&gt;        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;        


&lt;span class="c1"&gt;# Creating two different user instances from this class:
&lt;/span&gt;
&lt;span class="n"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Shameel&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;user2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Ammad&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this code, the &lt;code&gt;User&lt;/code&gt; class has 3 Class variables:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;max_login_attempts&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;password_min_length&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;system_name&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These are shared variables and are attached to every instance that will be created from the &lt;code&gt;User&lt;/code&gt; class. For example, the &lt;code&gt;user1&lt;/code&gt; instance will have following properties and values:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Feppvi1taawjkjrijsnop.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Feppvi1taawjkjrijsnop.png" alt="Python A" width="665" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, the &lt;code&gt;user2&lt;/code&gt; instance will have following properties and values:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Firx4uoahi6keu26xbefj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Firx4uoahi6keu26xbefj.png" alt="Python B" width="666" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, we do not have to define these 3 properties and values again and again for each instance. And if we want to &lt;em&gt;modify&lt;/em&gt; the value of any of these variables, we will just make changes in the value of the class variable defined inside the class, and all other instances will inherit the new value themselves.&lt;/p&gt;

&lt;h2&gt;
  
  
  Main Use Cases of using Class Variables:
&lt;/h2&gt;

&lt;p&gt;Since Class variables store data on the class level, here are some common use cases for which these variables are being used:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Sharing Constant or Configuration Values:&lt;/strong&gt; Storing data that should remain uniform across all instances, such as a company name, a default tax rate, or an API version. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tracking Class-Wide State:&lt;/strong&gt; Keeping tabs on metrics that span across individual objects, such as a counter tracking the total number of objects created. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Defining Default Values:&lt;/strong&gt; Setting baseline attributes that every new object inherits automatically but can optionally override later. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memory Efficiency:&lt;/strong&gt; Saving memory by maintaining a single copy of a variable instead of duplicating it inside every object.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Accessing the values of Class Variables
&lt;/h2&gt;

&lt;p&gt;There are two ways in which we can access the values of a Class variable.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Access value by class name
&lt;/h3&gt;

&lt;p&gt;Accessing the value by class name means that you access the value using the &lt;code&gt;Class.classVariableName&lt;/code&gt; pattern.&lt;/p&gt;

&lt;p&gt;For example, consider the same &lt;code&gt;User&lt;/code&gt; 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;User&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Class Variables 
&lt;/span&gt;    &lt;span class="n"&gt;max_login_attempts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; 
    &lt;span class="n"&gt;password_min_length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;
    &lt;span class="n"&gt;system_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hasabTech Portal&lt;/span&gt;&lt;span class="sh"&gt;"&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="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;  
        &lt;span class="c1"&gt;# Instance Variable  
&lt;/span&gt;        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;               

&lt;span class="c1"&gt;# Print the value of all class variables using class name:
&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Max Login Attempts: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;max_login_attempts&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Minimum Password Length: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;password_min_length&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;System Name: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;system_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2.Access value by instance name
&lt;/h3&gt;

&lt;p&gt;Accessing the value by instance name means that you access the value using the &lt;code&gt;instance.classVariableName&lt;/code&gt; pattern.&lt;/p&gt;

&lt;p&gt;For example, consider the same User class and it's instances &lt;code&gt;user1&lt;/code&gt; and &lt;code&gt;user2&lt;/code&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="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Class Variables 
&lt;/span&gt;    &lt;span class="n"&gt;max_login_attempts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; 
    &lt;span class="n"&gt;password_min_length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;
    &lt;span class="n"&gt;system_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hasabTech Portal&lt;/span&gt;&lt;span class="sh"&gt;"&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="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;  
        &lt;span class="c1"&gt;# Instance Variable  
&lt;/span&gt;        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;     

&lt;span class="c1"&gt;# Creating two different user instances from this class:
&lt;/span&gt;
&lt;span class="n"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Shameel&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;user2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Ammad&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   


&lt;span class="c1"&gt;# Print the value of all class variables using user1 instance:
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;User 1 Max Login Attempts: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;max_login_attempts&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;User 1 Min Pass Length: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;password_min_length&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;User 1 System Name: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;system_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Print the value of all class variables using user2 instance:
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;User 2 Max Login Attempts: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;user2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;max_login_attempts&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;User 2  Min Pass Length: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;user2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;password_min_length&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;User 2 System Name: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;user2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;system_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the case of an instance variable, you are bound to access it using the instance name. However, in case of accessing class variables, it's a matter of choice how one wants to access a class variable. As a general rule of thumb, it is preferred to use pattern # 1 i.e., access class variable using Class name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What are Class Variables?
&lt;/h3&gt;

&lt;p&gt;Class variables (also known as class attributes) &lt;strong&gt;are shared across all instances (objects) of a class&lt;/strong&gt;. They belong to the class itself, not to any specific instance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Two ways of accessing the values of a Class Variable
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Access by Class name: &lt;code&gt;Class.classVariableName&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Access by Instance name: &lt;code&gt;Instance.classVariableName&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Stay connected with hasabTech for more information:&lt;br&gt;
&lt;a href="https://hasab.tech/" rel="noopener noreferrer"&gt;Website&lt;/a&gt; | &lt;a href="https://web.facebook.com/hasabTech" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; | &lt;a href="https://www.linkedin.com/company/hasabtech" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; | &lt;a href="https://www.youtube.com/@hasabTech" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt; | &lt;a href="https://x.com/hasabTech" rel="noopener noreferrer"&gt;X (Twitter)&lt;/a&gt; | &lt;a href="https://www.tiktok.com/@hasabTech" rel="noopener noreferrer"&gt;TikTok&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>coding</category>
      <category>fullstack</category>
    </item>
    <item>
      <title>Python __init__ Method Explained: A Beginner's Guide to OOP</title>
      <dc:creator>Shameel Uddin</dc:creator>
      <pubDate>Sat, 13 Jun 2026 16:08:27 +0000</pubDate>
      <link>https://dev.to/shameel/python-init-method-explained-a-beginners-guide-to-oop-5bc6</link>
      <guid>https://dev.to/shameel/python-init-method-explained-a-beginners-guide-to-oop-5bc6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;__init__&lt;/code&gt; method helps us initialize objects automatically whenever they are created.&lt;/p&gt;

&lt;p&gt;Instead of manually assigning data to every object, Python allows us to automate the process using the &lt;code&gt;__init__&lt;/code&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Pain Point before Python &lt;strong&gt;init&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Imagine a simple class for a user. You create an object from that class, and then later assign attributes like name or email. That works, but it is manual.&lt;/p&gt;

&lt;p&gt;Consider the following 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;SimpleUser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;pass&lt;/span&gt; 
&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SimpleUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Shameel&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; 
&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;shameel@example.com&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we create an object and then manually assign attributes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;Every time we create an object, we must remember to add all required attributes manually.&lt;/p&gt;

&lt;p&gt;For example:&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="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SimpleUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;#If we forget to assign:
&lt;/span&gt;
&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our object becomes incomplete.&lt;/p&gt;

&lt;p&gt;This can cause errors later in the program.&lt;/p&gt;

&lt;h2&gt;
  
  
  Another Common Approach
&lt;/h2&gt;

&lt;p&gt;Developers often create a separate method:&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;SimpleUser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;set_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;

&lt;span class="n"&gt;Usage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SimpleUser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Shameel&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;shameel@example.com&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Although this works, there is still a risk.&lt;/p&gt;

&lt;p&gt;A developer might forget to call:&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="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_data&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the object will remain uninitialized.&lt;/p&gt;

&lt;p&gt;At first, this seems fine. But there is an obvious weakness. You can create the object and forget to call the setup method. Then you end up with an object that exists, but does not actually contain the data it is supposed to have.&lt;/p&gt;

&lt;p&gt;That is exactly the kind of error-prone pattern Python&lt;code&gt;__init__&lt;/code&gt; is designed to avoid.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Python &lt;strong&gt;init&lt;/strong&gt; Uses "self" to Assign Attributes
&lt;/h2&gt;

&lt;p&gt;Before understanding Python &lt;code&gt;__init__&lt;/code&gt; properly, it helps to be clear on how self works.&lt;/p&gt;

&lt;p&gt;Inside an instance method,&lt;code&gt;self&lt;/code&gt;refers to the specific object currently being worked on. So when you write something like:&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="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; 

&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you are attaching attributes directly to that object.&lt;/p&gt;

&lt;p&gt;So if an object is stored in a variable called &lt;code&gt;user&lt;/code&gt;, then inside the method, self is just another reference to that same object.&lt;/p&gt;

&lt;p&gt;Assigning values through &lt;code&gt;self&lt;/code&gt; means the object itself now carries those values.&lt;/p&gt;

&lt;p&gt;Nothing magical is happening. The object is simply receiving attributes through&lt;code&gt;self&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Python &lt;strong&gt;init&lt;/strong&gt; actually Is
&lt;/h2&gt;

&lt;p&gt;Python &lt;code&gt;__init__&lt;/code&gt; is a special method used for automatic object initialization.&lt;/p&gt;

&lt;p&gt;Its name has a special format with two underscores before and after it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Methods written in this style are often called Dunder Methods.&lt;/strong&gt;&lt;br&gt;
Dunder is short for double underscore methods. They are special methods in Python with predefined meanings.&lt;/p&gt;

&lt;p&gt;The&lt;code&gt;__init__&lt;/code&gt; method is written like any other method, but Python treats it specially. As soon as you create an object from a class Python &lt;code&gt;__init__&lt;/code&gt; runs automatically.&lt;/p&gt;

&lt;p&gt;That means instead of doing this in two separate steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create the object&lt;/li&gt;
&lt;li&gt;Manually call another method to set attributes&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;you can do it all at the time of object creation.&lt;/p&gt;
&lt;h2&gt;
  
  
  How Python &lt;strong&gt;init&lt;/strong&gt; Improves Object Creation
&lt;/h2&gt;

&lt;p&gt;Suppose you define a User class with an &lt;code&gt;__init__&lt;/code&gt; method that accepts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;email&lt;/li&gt;
&lt;li&gt;name&lt;/li&gt;
&lt;li&gt;role (with a default value)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That immediately gives you a much cleaner pattern&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;User&lt;/span&gt;&lt;span class="p"&gt;:&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="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now every time you create a &lt;code&gt;user&lt;/code&gt;, Python automatically runs this setup logic.&lt;/p&gt;

&lt;p&gt;So if you create an object with email and name, those values are assigned straight away. If you do not provide a role, it defaults to &lt;code&gt;user&lt;/code&gt;. This means your object starts out complete and predictable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Required and Optional Parameters in Python_&lt;em&gt;init&lt;/em&gt;_
&lt;/h2&gt;

&lt;p&gt;One useful thing about Python &lt;code&gt;__init__&lt;/code&gt; is that it lets you define which data is mandatory and which data is optional.&lt;/p&gt;

&lt;p&gt;In the example above:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;email is required&lt;/li&gt;
&lt;li&gt;name is required&lt;/li&gt;
&lt;li&gt;role is optional because it already has a default value&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That means object creation itself becomes a kind of contract. Anyone creating a &lt;code&gt;user&lt;/code&gt; must provide the important values. Optional values can still be customised when needed.&lt;/p&gt;

&lt;p&gt;This is one of the biggest reasons Python &lt;code&gt;__init__&lt;/code&gt; is so useful. It forces the right information to be provided at the right time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using Python &lt;strong&gt;init&lt;/strong&gt; for Validation
&lt;/h2&gt;

&lt;p&gt;Another major benefit of Python &lt;code&gt;__init__&lt;/code&gt; is validation.&lt;/p&gt;

&lt;p&gt;For example, if you expect an email address, you can check whether it contains the @ symbol before accepting it. If the format is wrong, you can stop object creation immediately.&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;User&lt;/span&gt;&lt;span class="p"&gt;:&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="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;@&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Invalid email&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is powerful because it prevents invalid objects from being created in the first place.&lt;/p&gt;

&lt;p&gt;You can also imagine stricter rules, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Allowing only business email addresses&lt;/li&gt;
&lt;li&gt;Rejecting common public domains&lt;/li&gt;
&lt;li&gt;Applying company-specific signup requirements&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The point is simple Python &lt;code&gt;__init__&lt;/code&gt; is not only for storing data. It is also the perfect place to protect the integrity of your objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Derived Attributes in Python &lt;strong&gt;init&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Sometimes an object is created with one piece of data, and from that data you want to generate something extra. That is where derived attributes come in.&lt;/p&gt;

&lt;p&gt;For example, if a user signs up with an email address, you might also want to store the email domain. Instead of asking for both separately, you can derive the domain from the email inside Python &lt;code&gt;__init__&lt;/code&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="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;domain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;@&lt;/span&gt;&lt;span class="sh"&gt;"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the email is something like &lt;code&gt;shameel@hasab.tech&lt;/code&gt;, then the derived domain becomes company.com.&lt;/p&gt;

&lt;p&gt;This is useful because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It avoids repeated work elsewhere in the code&lt;/li&gt;
&lt;li&gt;It keeps related logic together&lt;/li&gt;
&lt;li&gt;It ensures the derived value is always available on the object&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So Python &lt;code&gt;__init__&lt;/code&gt; can take input data and transform it into extra attributes that make the object more useful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Default Internal Attributes with Python &lt;strong&gt;init&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Another common pattern is assigning internal defaults that every object should have, regardless of what the caller provides.&lt;/p&gt;

&lt;p&gt;For a user object, that might mean attributes such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;is_active = True&lt;/li&gt;
&lt;li&gt;login_attempts = 0&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are not values the caller necessarily needs to pass in. They are part of the system's default behavior.&lt;br&gt;
This makes Python&lt;code&gt;__init__&lt;/code&gt; a convenient place to define the starting state of each object.&lt;/p&gt;

&lt;p&gt;Whenever a new user is created, those defaults are already in place. No extra setup is required later.&lt;/p&gt;
&lt;h2&gt;
  
  
  Auto-Generated Attributes in Python &lt;strong&gt;init&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Some attributes are not provided by the user at all. They are generated automatically by the system.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;/p&gt;

&lt;p&gt;A unique user ID A creation timestamp Some internal tracking value&lt;/p&gt;

&lt;p&gt;This is another natural use case for Python &lt;code&gt;__init__&lt;/code&gt; When the object is created, these values can be generated and attached immediately.&lt;/p&gt;

&lt;p&gt;A popular Python approach for unique identifiers is using a UUID. In real applications, this kind of auto-generated data is very common, especially for records that later connect to databases or APIs.&lt;/p&gt;

&lt;p&gt;What happens when an object is instantiated?&lt;/p&gt;

&lt;p&gt;This is the key thing to remember about Python &lt;code&gt;__init__&lt;/code&gt; it runs automatically during instantiation.&lt;/p&gt;

&lt;p&gt;When you write something like:&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="n"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;name@company.com&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Shameel&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python does not just create an empty object and stop there. It creates the object and then immediately calls &lt;code&gt;__init__&lt;/code&gt; for that object.&lt;/p&gt;

&lt;p&gt;At that moment:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;self refers to the newly created object&lt;/li&gt;
&lt;li&gt;The provided arguments are mapped to the parameters&lt;/li&gt;
&lt;li&gt;Assignments, validation, and extra logic run automatically&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So the object is initialized right away, not later.&lt;/p&gt;

&lt;p&gt;If you provide only the required values, optional ones use their defaults. If you provide a custom role, that custom value is stored instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Practical Example of Python &lt;strong&gt;init&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Here is the complete pattern all together:&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;User&lt;/span&gt;&lt;span class="p"&gt;:&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="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;user&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;@&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;raise&lt;/span&gt; &lt;span class="nc"&gt;ValueError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Invalid email&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;role&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;domain&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;@&lt;/span&gt;&lt;span class="sh"&gt;"&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="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_active&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;login_attempts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this design, each new object gets:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Required data stored automatically&lt;/li&gt;
&lt;li&gt;Optional data handled cleanly&lt;/li&gt;
&lt;li&gt;Validation before acceptance&lt;/li&gt;
&lt;li&gt;Derived attributes created instantly&lt;/li&gt;
&lt;li&gt;Default internal values assigned consistently&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is the real strength of Python&lt;code&gt;__init__&lt;/code&gt; It centralizes the setup of an object in one reliable place.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Python &lt;strong&gt;init&lt;/strong&gt; Beats Manual Setup Methods
&lt;/h2&gt;

&lt;p&gt;Manual setup methods can work, but they depend on discipline. A person creating the object has to remember the extra step every single time.&lt;/p&gt;

&lt;p&gt;That leads to several risks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;An object may be created without necessary attributes&lt;/li&gt;
&lt;li&gt;Validation may be skipped accidentally&lt;/li&gt;
&lt;li&gt;Default values may not be assigned consistently&lt;/li&gt;
&lt;li&gt;Different parts of the code may initialize objects differently
Python &lt;code&gt;__init__&lt;/code&gt; removes all of that uncertainty. The object cannot be properly created without passing through its initialization logic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In other words Python &lt;code&gt;__init__&lt;/code&gt; makes object creation safer, cleaner, and less repetitive.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Python &lt;strong&gt;init&lt;/strong&gt; Patterns Every Beginner Should Know
&lt;/h2&gt;

&lt;p&gt;When using Python &lt;code&gt;__init__&lt;/code&gt; these are the most important patterns to keep in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic attribute assignment Store incoming values directly on the object.&lt;/li&gt;
&lt;li&gt;Validation Check that incoming data is acceptable before storing it.&lt;/li&gt;
&lt;li&gt;Derived attributes Generate additional values from the provided input.&lt;/li&gt;
&lt;li&gt;Internal defaults Assign standard starting values that every object should have&lt;/li&gt;
&lt;li&gt;Auto-generated Values Create IDs, timestamps, or other system-generated properties.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final takeaway on Python &lt;strong&gt;init&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If you remember just one thing: &lt;code&gt;__init__&lt;/code&gt; is the automatic setup method for your objects. It is called as soon as an object is instantiated, and it exists to make sure the object starts with the right data, the right defaults, and the right checks.&lt;/p&gt;

&lt;p&gt;So instead of creating an object first and then manually attaching everything later, Python lets you define a proper initialization process once and reuse it every time.&lt;/p&gt;

&lt;p&gt;That is why it is such a core part of Object Oriented Programming in Python.&lt;br&gt;
Stay connected with hasabTech for more information:&lt;br&gt;
&lt;a href="https://hasab.tech/" rel="noopener noreferrer"&gt;Website&lt;/a&gt; | &lt;a href="https://web.facebook.com/hasabTech" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; | &lt;a href="https://www.linkedin.com/company/hasabtech" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; | &lt;a href="https://www.youtube.com/@hasabTech" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt; | &lt;a href="https://x.com/hasabTech" rel="noopener noreferrer"&gt;X (Twitter)&lt;/a&gt; | &lt;a href="https://www.tiktok.com/@hasabTech" rel="noopener noreferrer"&gt;TikTok&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>coding</category>
      <category>programming</category>
      <category>devops</category>
    </item>
    <item>
      <title>How JavaScript Works (Part 2) – Execution Context &amp; Call Stack Explained in Simple Words</title>
      <dc:creator>Shameel Uddin</dc:creator>
      <pubDate>Mon, 02 Mar 2026 10:47:03 +0000</pubDate>
      <link>https://dev.to/shameel/how-javascript-works-part-2-execution-context-call-stack-explained-in-simple-words-2j26</link>
      <guid>https://dev.to/shameel/how-javascript-works-part-2-execution-context-call-stack-explained-in-simple-words-2j26</guid>
      <description>&lt;p&gt;In Part 1, we explored how JavaScript runs under the hood and discussed the JavaScript engine and runtime environment.&lt;/p&gt;

&lt;p&gt;In this part, we’re going deeper.&lt;/p&gt;

&lt;p&gt;We will understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is Execution Context?&lt;/li&gt;
&lt;li&gt;What are the different phases and types of an Execution Context?&lt;/li&gt;
&lt;li&gt;What is the Call Stack?&lt;/li&gt;
&lt;li&gt;How functions are executed internally?
And most importantly we’ll visualise everything with a simple code example.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s begin.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an Execution Context in JavaScript?
&lt;/h2&gt;

&lt;p&gt;Whenever JavaScript runs your code, it creates a special environment called an &lt;strong&gt;Execution Context&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Think of it as a &lt;strong&gt;container&lt;/strong&gt; where JavaScript keeps everything needed to execute your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Phases of Execution Context
&lt;/h2&gt;

&lt;p&gt;Each execution context goes through two phases:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Creation Phase (Memory Allocation Phase)
&lt;/h3&gt;

&lt;p&gt;Before executing your code, JavaScript:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Scans the code&lt;/li&gt;
&lt;li&gt;Allocates memory for variables&lt;/li&gt;
&lt;li&gt;Stores function definitions in memory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Important behavior:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Variables declared with &lt;code&gt;var&lt;/code&gt;are initialized with &lt;code&gt;undefined&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Functions are stored entirely in memory&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are hoisted but kept in a temporal dead zone
No code is executed here. Only memory setup happens.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Execution Phase
&lt;/h3&gt;

&lt;p&gt;In this phase, JavaScript:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Executes code line by line&lt;/li&gt;
&lt;li&gt;Assigns actual values to variables&lt;/li&gt;
&lt;li&gt;Runs functions when invoked&lt;/li&gt;
&lt;li&gt;Evaluates expressions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is where real execution happens.&lt;/p&gt;

&lt;h2&gt;
  
  
  Types of Execution Context
&lt;/h2&gt;

&lt;p&gt;There are two main types of execution context:&lt;/p&gt;

&lt;h3&gt;
  
  
  1.Global Execution Context (GEC)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Created when JavaScript starts running your file&lt;/li&gt;
&lt;li&gt;Exists only once and stays in the Call Stack until the program closes&lt;/li&gt;
&lt;li&gt;Represents the global scope&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2.Function Execution Context (FEC)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Created whenever a function is called or invoked&lt;/li&gt;
&lt;li&gt;Each function call creates a new execution context&lt;/li&gt;
&lt;li&gt;Has its own memory and thread of execution&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What’s Inside an Execution Context?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Thread of Execution
&lt;/h3&gt;

&lt;p&gt;This is the order in which JavaScript runs your code line by line.&lt;/p&gt;

&lt;p&gt;JavaScript is (initially) single-threaded, meaning it executes one line at a time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Memory
&lt;/h3&gt;

&lt;p&gt;Each execution context has its own memory.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Global context → Global memory&lt;/li&gt;
&lt;li&gt;Function context → Local memory&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why variables inside functions are not accessible outside (unless returned).&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the Call Stack?
&lt;/h2&gt;

&lt;p&gt;JavaScript uses a special memory structure to keep track of all execution contexts called the &lt;strong&gt;Call Stack&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It follows LIFO structure, i.e., "Last In, First Out" (like a stack of plates).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The last function added to the stack, will be executed first, and will be popped out of the stack first.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How all of this works: Step-by-Step!
&lt;/h2&gt;

&lt;p&gt;Let’s understand this using a simple example.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;firstName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Munzah&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shah&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;27&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;designation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Software Engineer&lt;/span&gt;&lt;span class="dl"&gt;"&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;createFullName&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;firstName&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;;&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;displayUserDetails&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Full Name:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;createFullName&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Age:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Designation:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;designation&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;displayUserDetails&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step-by-Step Execution Breakdown
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Global Execution Context is Created
&lt;/h3&gt;

&lt;p&gt;When the file runs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Global Execution Context (GEC) is created&lt;/li&gt;
&lt;li&gt;It is pushed into the Call Stack&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Call Stack:&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="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Global&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Creation Phase of Global Context
&lt;/h2&gt;

&lt;p&gt;Memory is allocated:&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="nx"&gt;firstName&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;
&lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;
&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;
&lt;span class="nx"&gt;designation&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;
&lt;span class="nx"&gt;createFullName&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;definition&lt;/span&gt;
&lt;span class="nx"&gt;displayUserDetails&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;definition&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Execution Phase Begins
&lt;/h2&gt;

&lt;p&gt;Now values are assigned:&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="nx"&gt;firstName&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Munzah&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;lastName&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shah&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="mi"&gt;27&lt;/span&gt;
&lt;span class="nx"&gt;designation&lt;/span&gt; &lt;span class="err"&gt;→&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Software Engineer&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Functions are still not executed just stored.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Function Invocation Happens
&lt;/h2&gt;

&lt;p&gt;When this line runs:&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="nf"&gt;displayUserDetails&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A new &lt;strong&gt;Function Execution Context&lt;/strong&gt; is created.&lt;/p&gt;

&lt;p&gt;Call Stack becomes:&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="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;displayUserDetails&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Global&lt;/span&gt;             &lt;span class="o"&gt;|&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now JavaScript executes &lt;code&gt;displayUserDetails.&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Inside displayUserDetails()
&lt;/h2&gt;

&lt;p&gt;The thread of execution encounters another function call:&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="nf"&gt;createFullName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Another Function Execution ExecutionContext is created.&lt;/p&gt;

&lt;p&gt;Call Stack now looks like this:&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="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;createFullName&lt;/span&gt;     &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;displayUserDetails&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Global&lt;/span&gt;             &lt;span class="o"&gt;|&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 6: createFullName Executes
&lt;/h2&gt;

&lt;p&gt;It returns:&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Munzah Shah&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once function is completed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Its result is returned to the parent execution context.&lt;/li&gt;
&lt;li&gt;And It's execution context is popped out from the stack.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So now, call Stack becomes:&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="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;displayUserDetails&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Global&lt;/span&gt;             &lt;span class="o"&gt;|&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 7: Remaining Code In displayUserDetails() Function Executes
&lt;/h2&gt;

&lt;p&gt;Now:&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="nx"&gt;Age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;27&lt;/span&gt;
&lt;span class="nx"&gt;Designation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Software&lt;/span&gt; &lt;span class="nx"&gt;Engineer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After finishing the execution of this function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;displayUserDetails() context is removed&lt;/li&gt;
&lt;li&gt;Stack pointer goes back to Global&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Call Stack:&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="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;Global&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Important Rule to Remember
&lt;/h2&gt;

&lt;p&gt;JavaScript always executes the function that is on the top of the Call Stack.&lt;/p&gt;

&lt;p&gt;That’s why it follows LIFO.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Output of This Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Full&lt;/span&gt; &lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Munzah&lt;/span&gt; &lt;span class="nx"&gt;Shah&lt;/span&gt;
&lt;span class="nx"&gt;Age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;27&lt;/span&gt;
&lt;span class="nx"&gt;Designation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Software&lt;/span&gt; &lt;span class="nx"&gt;Engineer&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why Understanding This is Important
&lt;/h2&gt;

&lt;p&gt;If you understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Execution Context&lt;/li&gt;
&lt;li&gt;Call Stack&lt;/li&gt;
&lt;li&gt;Creation Phase&lt;/li&gt;
&lt;li&gt;Execution Phase&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You will easily understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hoisting&lt;/li&gt;
&lt;li&gt;Scope&lt;/li&gt;
&lt;li&gt;Closures&lt;/li&gt;
&lt;li&gt;Asynchronous JavaScript&lt;/li&gt;
&lt;li&gt;Event Loop&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And that’s when JavaScript truly starts making sense.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript creates a &lt;strong&gt;Global Execution Context (GEC)&lt;/strong&gt; when a program starts running.&lt;/li&gt;
&lt;li&gt;Every execution context goes through &lt;strong&gt;two phases&lt;/strong&gt;:&lt;/li&gt;
&lt;li&gt;Creation Phase (memory allocation)&lt;/li&gt;
&lt;li&gt;Execution Phase (code runs line by line)&lt;/li&gt;
&lt;li&gt;Variables and function definitions are stored in memory during the creation phase.&lt;/li&gt;
&lt;li&gt;Each function call creates a new Function Execution Context (FEC).&lt;/li&gt;
&lt;li&gt;Every execution context contains:&lt;/li&gt;
&lt;li&gt;Its own memory&lt;/li&gt;
&lt;li&gt;A thread of execution&lt;/li&gt;
&lt;li&gt;JavaScript uses a &lt;strong&gt;Call Stack&lt;/strong&gt; to manage execution contexts.&lt;/li&gt;
&lt;li&gt;The Call Stack follows the **Last In, First Out (LIFO) **principle.&lt;/li&gt;
&lt;li&gt;The function at the top of the stack is executed first.&lt;/li&gt;
&lt;li&gt;Once a function finishes execution, it is removed (popped) from the stack.&lt;/li&gt;
&lt;li&gt;The Global Execution Context remains until the program finishes running.&lt;/li&gt;
&lt;li&gt;Understanding execution context and call stack makes advanced concepts like hoisting, closures, and async JavaScript easier to understand.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Stay connected with hasabTech for more information:&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;a href="https://hasab.tech/" rel="noopener noreferrer"&gt;Website&lt;/a&gt; | &lt;a href="https://web.facebook.com/hasabTech" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; | &lt;a href="https://www.linkedin.com/company/hasabtech" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; | &lt;a href="https://www.youtube.com/@hasabTech" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt; | &lt;a href="https://x.com/hasabTech" rel="noopener noreferrer"&gt;X (Twitter)&lt;/a&gt; | &lt;a href="https://www.tiktok.com/@hasabTech" rel="noopener noreferrer"&gt;TikTok&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>coding</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Instance Variables and Instance Methods in Python</title>
      <dc:creator>Shameel Uddin</dc:creator>
      <pubDate>Fri, 20 Feb 2026 09:29:14 +0000</pubDate>
      <link>https://dev.to/shameel/instance-variables-and-instance-methods-in-python-51o0</link>
      <guid>https://dev.to/shameel/instance-variables-and-instance-methods-in-python-51o0</guid>
      <description>&lt;p&gt;In Object-Oriented Programming (OOP), two of the most important concepts are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Instance Variables&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Instance Methods&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you don’t understand these properly, OOP will always feel confusing.&lt;/p&gt;

&lt;p&gt;So let’s break everything down step by step in the simplest way possible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s Start with a Simple Real-Life Example
&lt;/h2&gt;

&lt;p&gt;Imagine a classroom. Learn How Instance Variables and Methods Actually Work in Python&lt;/p&gt;

&lt;p&gt;You (teacher) are the class blueprint.&lt;br&gt;
Your students are objects.&lt;/p&gt;

&lt;p&gt;Every student:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Has a name&lt;/li&gt;
&lt;li&gt;Has a roll number&lt;/li&gt;
&lt;li&gt;Has marks&lt;/li&gt;
&lt;li&gt;Can perform actions (submit homework, give test, etc.)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now here’s the important part:&lt;/p&gt;

&lt;p&gt;Even though all students come from the same class,&lt;br&gt;
each student has their own data.&lt;/p&gt;

&lt;p&gt;That “own data” is exactly what we call:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Instance Variables&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And the actions they perform?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Instance Methods&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  What is an Instance Method in Python?
&lt;/h2&gt;

&lt;p&gt;An instance method is a method that belongs to an object and works on that specific object.&lt;/p&gt;
&lt;h3&gt;
  
  
  Definition (Simple Words)
&lt;/h3&gt;

&lt;p&gt;An instance method:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is defined inside a class&lt;/li&gt;
&lt;li&gt;Has &lt;code&gt;self&lt;/code&gt; as its first parameter&lt;/li&gt;
&lt;li&gt;Can access and modify instance variables&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Why Do We Need self?
&lt;/h3&gt;

&lt;p&gt;When we create multiple objects from a class, each object has its own data.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self&lt;/code&gt; tells Python:&lt;/p&gt;

&lt;p&gt;Work with the current object that is calling this method&lt;/p&gt;

&lt;p&gt;Example&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;User&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;activate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_active&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we create two 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="n"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;user2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we call&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="n"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;activate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python automatically does this internally:&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="n"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;activate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That means:&lt;/p&gt;

&lt;p&gt;self = user1&lt;br&gt;
So instance methods always operate on the object that calls them&lt;/p&gt;
&lt;h2&gt;
  
  
  What is an Instance Variable in Python?
&lt;/h2&gt;

&lt;p&gt;An instance variable is a variable that belongs to a specific object.&lt;/p&gt;

&lt;p&gt;It is created using:&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="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;variable_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Important Concept
&lt;/h3&gt;

&lt;p&gt;Instance variables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Store data&lt;/li&gt;
&lt;li&gt;Are unique for each object&lt;/li&gt;
&lt;li&gt;Exist inside the object&lt;/li&gt;
&lt;li&gt;Can be different for different objects
&lt;strong&gt;Example&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;set_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we call:&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="n"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;shameel@hasabtech.com&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It becomes:&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="n"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;shameel@hasabtech.com&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;email&lt;/code&gt; belongs only to &lt;code&gt;user1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If we check &lt;code&gt;user2&lt;/code&gt;, it still does NOT have email until we set it.&lt;/p&gt;

&lt;p&gt;That is why instance variables are object-specific.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complete Example with Deep Explanation
&lt;/h3&gt;

&lt;p&gt;Let’s build a proper example and understand what happens step by step.&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;User&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;activate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_active&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;deactivate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_active&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;set_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show_status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; is &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;active&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_active&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;not active&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 1: Creating Objects (Instances)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;user1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;user2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we execute this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Python creates two separate objects in memory&lt;/li&gt;
&lt;li&gt;Each object has its own space to store data&lt;/li&gt;
&lt;li&gt;Both objects have access to all instance methods&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Important: Object and Instance mean the same thing.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Happens Inside Memory?
&lt;/h3&gt;

&lt;p&gt;Even though both objects come from the same class blueprint:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;user1&lt;/code&gt; has its own data storage&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;user2&lt;/code&gt; has its own data storage&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They do NOT share instance variables.&lt;/p&gt;

&lt;p&gt;This is the beauty of OOP.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding Instance Methods in Action
&lt;/h2&gt;

&lt;p&gt;Now let’s call:&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="n"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;shameel@hasabtech.com&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Python sees user1 &lt;/li&gt;
&lt;li&gt;It passes user1 automatically as self &lt;/li&gt;
&lt;li&gt;Inside method:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Becomes&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="n"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;shameel@hasabtech.com&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;user1&lt;/code&gt; has email&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;user2&lt;/code&gt; still has nothing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This proves instance variables belong to objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding More Data to Objects
&lt;/h2&gt;

&lt;p&gt;Now:&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="n"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;activate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;user2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deactivate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates:&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="n"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_active&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;
&lt;span class="n"&gt;user2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_active&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="bp"&gt;False&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each object has its own &lt;code&gt;is_active&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;They are completely independent&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This independence is the core idea of instance variables.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Methods are Called Instance Methods?
&lt;/h2&gt;

&lt;p&gt;Because they:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Work with instance variables&lt;/li&gt;
&lt;li&gt;Depend on the object&lt;/li&gt;
&lt;li&gt;Use self to access object data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example:&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;def&lt;/span&gt; &lt;span class="nf"&gt;show_status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt; is &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;active&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_active&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;not active&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reads &lt;code&gt;self.email&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Reads &lt;code&gt;self.is_active&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Prints data of that specific object&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So this method behaves differently for each object.&lt;/p&gt;

&lt;p&gt;That’s why it is called an instance method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Beginner Confusion
&lt;/h2&gt;

&lt;p&gt;Many students think:&lt;br&gt;
Is self a keyword?&lt;br&gt;
No.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;self&lt;/code&gt; is just a naming convention.&lt;/p&gt;

&lt;p&gt;You can write:&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;def&lt;/span&gt; &lt;span class="nf"&gt;activate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myobject&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;def activate(myobject):&lt;/p&gt;

&lt;p&gt;But by convention, we always write &lt;code&gt;self&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why This Concept is Very Important?
&lt;/h2&gt;

&lt;p&gt;If you understand instance variables and instance methods, you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build login systems&lt;/li&gt;
&lt;li&gt;Create user management systems&lt;/li&gt;
&lt;li&gt;Understand Django models&lt;/li&gt;
&lt;li&gt;Work with APIs&lt;/li&gt;
&lt;li&gt;Design real-world applications&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without understanding self, OOP will feel complicated.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Understanding
&lt;/h2&gt;

&lt;p&gt;Whenever:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You attach data using &lt;code&gt;self&lt;/code&gt; → It becomes an instance variable.&lt;/li&gt;
&lt;li&gt;You define a method with &lt;code&gt;self&lt;/code&gt; → It becomes an instance method.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each object:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Has its own data&lt;/li&gt;
&lt;li&gt;Shares method structure&lt;/li&gt;
&lt;li&gt;Behaves independently&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Object = Instance. They mean the same thing.&lt;/li&gt;
&lt;li&gt;Instance Variables are the "adjectives" (data) that describe the object.&lt;/li&gt;
&lt;li&gt;Instance Methods are the "verbs" (actions) the object can perform.&lt;/li&gt;
&lt;li&gt;self is the bridge that connects the method to the specific object’s data.&lt;/li&gt;
&lt;li&gt;Each object has its own copy of instance variables&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Stay connected with hasabTech for more information:&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;a href="https://hasab.tech/" rel="noopener noreferrer"&gt;Website&lt;/a&gt; | &lt;a href="https://web.facebook.com/hasabTech" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; | &lt;a href="https://www.linkedin.com/company/hasabtech" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; | &lt;a href="https://www.youtube.com/@hasabTech" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt; | &lt;a href="https://x.com/hasabTech" rel="noopener noreferrer"&gt;X (Twitter)&lt;/a&gt; | &lt;a href="https://www.tiktok.com/@hasabTech" rel="noopener noreferrer"&gt;TikTok&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>coding</category>
      <category>oop</category>
    </item>
    <item>
      <title>Understanding self in Python OOP: A Beginner Friendly Guide</title>
      <dc:creator>Shameel Uddin</dc:creator>
      <pubDate>Thu, 12 Feb 2026 11:31:49 +0000</pubDate>
      <link>https://dev.to/shameel/understanding-self-in-python-oop-a-beginner-friendly-guide-573o</link>
      <guid>https://dev.to/shameel/understanding-self-in-python-oop-a-beginner-friendly-guide-573o</guid>
      <description>&lt;p&gt;One of the biggest "roadblocks" for Python beginners is the &lt;code&gt;self&lt;/code&gt; keyword. You see it in almost every class, it’s the first argument in every method, and yet, we don't seem to pass any value to it when we call the function.&lt;/p&gt;

&lt;p&gt;If you’ve ever wondered:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What does &lt;code&gt;self&lt;/code&gt; actually do?&lt;/li&gt;
&lt;li&gt;Why do I have to type it every single time?&lt;/li&gt;
&lt;li&gt;How does Python know which object I’m talking about?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then this Blog is for you. Let’s break it down.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Class and a Method?
&lt;/h2&gt;

&lt;p&gt;Before we talk about &lt;code&gt;self&lt;/code&gt;, remember the relationship between a Class and  an Object.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Class&lt;/strong&gt;: A blueprint (like a drawing of a house).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Object (Instance)&lt;/strong&gt;: The actual house built from that blueprint.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you build five houses from the same blueprint, they all have the same structure, but they have different owners and different furniture inside. &lt;code&gt;self&lt;/code&gt; is the way the house "identifies" itself.&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nx"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;set_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;User&lt;/code&gt; → Class&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;set_email()&lt;/code&gt; → Method&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is &lt;code&gt;self&lt;/code&gt;? (The Simple Definition)
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;self&lt;/code&gt;represents the specific instance (object) of the class.&lt;/p&gt;

&lt;p&gt;When you define a method inside a class, Python needs a way to distinguish between different objects.&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Robot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nx"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;introduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello, I am {self.name}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, &lt;code&gt;self.name&lt;/code&gt; tells Python: Look for the name attribute inside THIS specific robot, not all robots in the world.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Secret Behind Method Calls
&lt;/h2&gt;

&lt;p&gt;This is where most beginners get confused. Look at these two ways to do the exact same thing:&lt;/p&gt;

&lt;h3&gt;
  
  
  Method 1: The Manual Way (Internal)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ammad@hasab.tech&lt;/span&gt;&lt;span class="dl"&gt;"&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="nf"&gt;set_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shameel@hasab.tech&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We manually pass the object&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;user1&lt;/code&gt; becomes &lt;code&gt;self&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;This helps us understand what Python does internally&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Method 2: The Pythonic Way (Standard)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ammad@hasab.tech&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;user2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shameell@hasab.tech&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Behind the scenes, Python converts this into:&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="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ammad@hasab.tech&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This method is simpler and mostly used in real projects.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How &lt;code&gt;self&lt;/code&gt; Works with Different Objects
&lt;/h2&gt;

&lt;p&gt;When this line runs:&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="nx"&gt;user1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ammad@hasab.tech&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;self&lt;/code&gt;→ &lt;code&gt;user1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;user1.email&lt;/code&gt; → &lt;code&gt;"ammad@hasab.tech"&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When this line runs:&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="nx"&gt;user2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;shameel@hasab.tech&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;self&lt;/code&gt; → &lt;code&gt;user2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;user2.email&lt;/code&gt; → &lt;code&gt;"shameel@hasab.tech"&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This shows that self ensures each user instance uses it’s own data or value, which is the core idea of Object Oriented Programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why &lt;code&gt;self&lt;/code&gt; Is Important
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;self&lt;/code&gt; allows Python to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Identify &lt;strong&gt;which object is calling the method&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Store data &lt;strong&gt;separately for each object&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Avoid data overwriting between objects&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without &lt;code&gt;self&lt;/code&gt;, Python would not know where to store object-specific values.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Beginner Mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Thinking &lt;code&gt;self&lt;/code&gt; is a keyword (it’s not, but it’s a strong convention)&lt;/li&gt;
&lt;li&gt;Forgetting to add &lt;code&gt;self&lt;/code&gt; as the first parameter&lt;/li&gt;
&lt;li&gt;Confusing instance variables with class variables&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Always remember:&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="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;variable_name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;means the variable belongs to &lt;strong&gt;that specific object&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;self&lt;/code&gt; represents the &lt;strong&gt;current object&lt;/strong&gt; in Python&lt;/li&gt;
&lt;li&gt;It helps Python identify &lt;strong&gt;which object is calling a method&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;A method can be called in &lt;strong&gt;two ways&lt;/strong&gt;:&lt;/li&gt;
&lt;li&gt;Using class name (manual)&lt;/li&gt;
&lt;li&gt;Using object name (automatic and common)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;self&lt;/code&gt; changes depending on the object calling the method&lt;/li&gt;
&lt;li&gt;It allows each object to maintain &lt;strong&gt;its own data independently&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Understanding &lt;code&gt;self&lt;/code&gt; is a foundation of Python OOP, and once you grasp it, many advanced concepts become much easier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Stay connected with hasabTech for more information:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://hasab.tech/" rel="noopener noreferrer"&gt;Website&lt;/a&gt; | &lt;a href="https://web.facebook.com/hasabTech" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; | &lt;a href="https://www.linkedin.com/company/hasabtech" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; | &lt;a href="https://www.youtube.com/@hasabTech" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt; | &lt;a href="https://x.com/hasabTech" rel="noopener noreferrer"&gt;X (Twitter)&lt;/a&gt; | &lt;a href="https://www.tiktok.com/@hasabTech" rel="noopener noreferrer"&gt;TikTok&lt;/a&gt;&lt;/p&gt;

</description>
      <category>oop</category>
      <category>python</category>
      <category>programming</category>
      <category>coding</category>
    </item>
    <item>
      <title>Understanding Classes and Objects in Python</title>
      <dc:creator>Shameel Uddin</dc:creator>
      <pubDate>Mon, 02 Feb 2026 18:52:38 +0000</pubDate>
      <link>https://dev.to/shameel/understanding-classes-and-objects-in-python-3f50</link>
      <guid>https://dev.to/shameel/understanding-classes-and-objects-in-python-3f50</guid>
      <description>&lt;p&gt;Now it’s time to clearly understand what a class is and what an object is in Object-Oriented Programming (OOP) using Python.&lt;/p&gt;

&lt;p&gt;These two concepts are the foundation of OOP, and once you understand them, everything else becomes much easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is a Class in Python?
&lt;/h2&gt;

&lt;p&gt;In simple words, a class is a blueprint or a template for creating objects that share common properties.&lt;/p&gt;

&lt;p&gt;To understand this, let’s use a real-world example.&lt;/p&gt;

&lt;p&gt;As discussed in our previous blog, first the concept of a human being was created, and then you and I were created from that concept. That concept is like a class, and our individual existence is like an object.&lt;/p&gt;

&lt;p&gt;In Python terms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A class defines the structure (attributes) and behavior (methods).&lt;/li&gt;
&lt;li&gt;It describes what an object will look like&lt;/li&gt;
&lt;li&gt;It doesn’t occupy memory by itself because it’s just a concept.
So basically, a class is just a blueprint, not a real thing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Is an Object in Python?
&lt;/h2&gt;

&lt;p&gt;An object is the actual house built from that blueprint. In programming, we call this an instance.&lt;/p&gt;

&lt;p&gt;You can think of it this way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Class = Blueprint&lt;/li&gt;
&lt;li&gt;Object = Instance created from that blueprint&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Python, you’ll hear people use "object" and "instance" interchangeably. Don't let that confuse you they refer to the same thing: the real, usable version of a class.&lt;/p&gt;

&lt;h2&gt;
  
  
  Built-in Classes in Python
&lt;/h2&gt;

&lt;p&gt;You might be surprised to learn that you’ve been using classes since Day 1. Every data type in Python—strings, integers, lists, and dictionaries is actually a class.&lt;/p&gt;

&lt;p&gt;Try running this in your terminal:&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="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
          &lt;span class="c1"&gt;//Output: &amp;lt;class 'str'&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See that? &lt;code&gt;str&lt;/code&gt; is a class provided by Python. When you create a string like &lt;code&gt;name = "Shameel"&lt;/code&gt;, you are actually creating an object of the &lt;code&gt;str&lt;/code&gt; class.&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="nx"&gt;s0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;
            &lt;span class="nx"&gt;s1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shameel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;str&lt;/code&gt; is the class&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;str()&lt;/code&gt; creates an object&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The parentheses &lt;code&gt;()&lt;/code&gt; mean that an object of the class is being created&lt;br&gt;
So:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;str → blueprint&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;str() → object (instance)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why we say class and instance (object) are closely related.&lt;/p&gt;

&lt;h2&gt;
  
  
  Literal Syntax in Python
&lt;/h2&gt;

&lt;p&gt;When you write:&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="nx"&gt;s2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shameel&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is called shorthand literal syntax.&lt;/p&gt;

&lt;p&gt;Behind the scenes, Python still creates an object of the &lt;code&gt;str&lt;/code&gt;class. Python just hides this complexity to make the language easy and beginner-friendly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Modules and Built-in Classes
&lt;/h2&gt;

&lt;p&gt;Every class in Python belongs to a module.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The file you run is also considered a module&lt;/li&gt;
&lt;li&gt;Built-in classes like &lt;code&gt;str&lt;/code&gt; belong to the builtins module&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s why when you inspect a string class, you’ll see it comes from &lt;code&gt;builtins&lt;/code&gt;, even though you didn’t import anything manually.&lt;/p&gt;

&lt;h2&gt;
  
  
  Objects and Memory in Python
&lt;/h2&gt;

&lt;p&gt;When you create an object, Python carves out a little piece of your computer's RAM to store it. Each object gets a unique "home address."&lt;/p&gt;

&lt;p&gt;If you print a custom object, you’ll often see something like this: &lt;code&gt;&amp;lt;__main__.User object at 0x106558c20&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
That long code at the end &lt;code&gt;(0x1065...)&lt;/code&gt; is the physical memory address where that specific object lives.&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;__main__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt; &lt;span class="nx"&gt;at&lt;/span&gt; &lt;span class="mh"&gt;0x106558c20&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This address shows where the object exists in memory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a Custom Class in Python
&lt;/h2&gt;

&lt;p&gt;Now let’s move from built-in classes to custom classes.&lt;/p&gt;

&lt;p&gt;To create your own class in Python, you use the &lt;code&gt;class&lt;/code&gt; keyword:&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nx"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;User&lt;/code&gt; is the class&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;pass&lt;/code&gt; means the class is empty for now
To create an object from this class:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;u1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;()&lt;/code&gt; tells Python to create an object from the &lt;code&gt;User&lt;/code&gt; class and store it in the variable &lt;code&gt;u1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can create multiple objects from the same class:&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="nx"&gt;u2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One class&lt;/li&gt;
&lt;li&gt;Multiple objects&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just like one concept of humans, but many human beings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding &lt;strong&gt;main&lt;/strong&gt; in Python Output
&lt;/h2&gt;

&lt;p&gt;When you see output like:&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;__main__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;__main__&lt;/code&gt; is the current file being executed&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;User&lt;/code&gt; is the class defined in that file&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the class were defined in another file, you would see the module name instead of &lt;code&gt;__main__&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking an Object’s Class with type()
&lt;/h2&gt;

&lt;p&gt;You can check the class of any object using:&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="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;u1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="nc"&gt;__main__&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This confirms that &lt;code&gt;u1&lt;/code&gt; is an object of the &lt;code&gt;User&lt;/code&gt; class.&lt;br&gt;
Checking Object Instances with &lt;code&gt;isinstance()&lt;/code&gt;&lt;br&gt;
This is a great way to "double-check" an object. In programming, any function starting with "is" usually returns a True or False (Boolean).&lt;/p&gt;

&lt;p&gt;Examples:&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="nf"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s1&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="c1"&gt;// True&lt;/span&gt;
&lt;span class="nf"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;s1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// False&lt;/span&gt;
&lt;span class="nf"&gt;isinstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;u1&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="c1"&gt;// True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In programming, when a sentence starts with “Is”, the result is usually a Boolean value (&lt;code&gt;True&lt;/code&gt; or &lt;code&gt;False&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;This method is widely used in Python to validate object types.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Object-Oriented Programming (OOP) in Python is built around classes and objects.&lt;/li&gt;
&lt;li&gt;A class is a blueprint that defines structure and behavior.&lt;/li&gt;
&lt;li&gt;An object (instance) is a real entity created from a class.&lt;/li&gt;
&lt;li&gt;Python’s built-in data types like str, int, list, and dict are actually classes.&lt;/li&gt;
&lt;li&gt;Objects are created using parentheses &lt;code&gt;()&lt;/code&gt; and stored in memory.&lt;/li&gt;
&lt;li&gt;You can create your own custom classes using the &lt;code&gt;class&lt;/code&gt; keyword.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;type()&lt;/code&gt; to check an object’s class.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;isinstance()&lt;/code&gt; to verify whether an object belongs to a specific class.&lt;/li&gt;
&lt;li&gt;Understanding classes and objects is essential for mastering Python OOP.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Stay connected with hasabTech for more information:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://hasab.tech/" rel="noopener noreferrer"&gt;Website&lt;/a&gt; | &lt;a href="https://web.facebook.com/hasabTech" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; | &lt;a href="https://www.linkedin.com/company/hasabtech" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; | &lt;a href="https://www.youtube.com/@hasabTech" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt; | &lt;a href="https://x.com/hasabTech" rel="noopener noreferrer"&gt;X (Twitter)&lt;/a&gt; | &lt;a href="https://www.tiktok.com/@hasabTech" rel="noopener noreferrer"&gt;TikTok&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>webdev</category>
      <category>programming</category>
      <category>codepen</category>
    </item>
    <item>
      <title>Python OOP Prerequisites: The Essential Checklist for Beginners</title>
      <dc:creator>Shameel Uddin</dc:creator>
      <pubDate>Wed, 28 Jan 2026 14:20:20 +0000</pubDate>
      <link>https://dev.to/shameel/python-oop-prerequisites-the-essential-checklist-for-beginners-236e</link>
      <guid>https://dev.to/shameel/python-oop-prerequisites-the-essential-checklist-for-beginners-236e</guid>
      <description>&lt;p&gt;Before diving head-first into the world of &lt;strong&gt;Object-Oriented Programming (OOP)&lt;/strong&gt;, you need a solid grasp of a few "bread and butter" Python concepts. Think of it like building a house: you can't install the roof (OOP) until you’ve laid the foundation (Basics).&lt;/p&gt;

&lt;p&gt;Don’t worry this series is designed for beginners. You don’t need to be a senior dev to get started, but having these few tools in your belt will make the transition to OOP feel like a breeze rather than a climb.&lt;/p&gt;

&lt;h2&gt;
  
  
  Core Python Basics You Should Know
&lt;/h2&gt;

&lt;p&gt;To get the most out of this OOP series, ensure you're comfortable with the following three pillars:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Variables and Data Types
&lt;/h3&gt;

&lt;p&gt;In OOP, we store data inside "Objects." To do that, you must understand how Python handles data. You should be familiar with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Strings:&lt;/strong&gt; For names and descriptions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Integers/Floats:&lt;/strong&gt; For counts, ages, or prices.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Booleans:&lt;/strong&gt; For "True/False" logic (essential for state management).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lists:&lt;/strong&gt; For storing collections of objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why it matters:&lt;/strong&gt; In OOP, these variables will eventually become Attributes the characteristics that define your objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Functions in Python
&lt;/h3&gt;

&lt;p&gt;Functions are the "actions" of your code. Before moving to OOP, you should know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to define a function (&lt;code&gt;def&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;How to pass arguments (positional and keyword).&lt;/li&gt;
&lt;li&gt;How to return values to use elsewhere.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why it matters:&lt;/strong&gt; In the OOP world, &lt;strong&gt;functions living inside a class are called Methods&lt;/strong&gt;. If you can write a function, you’re already 80% of the way to writing a class method.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Working with Dictionaries
&lt;/h3&gt;

&lt;p&gt;You should understand how Key-Value pairs work because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dictionaries represent structured data.&lt;/li&gt;
&lt;li&gt;Internally, Python actually uses dictionaries to store object attributes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you can pull a value from a dictionary using a key, you'll find the logic of accessing object properties very familiar.&lt;/p&gt;

&lt;h2&gt;
  
  
  What You Do NOT Need to Know
&lt;/h2&gt;

&lt;p&gt;It’s easy to feel overwhelmed by the vast Python ecosystem. You do not need to master these before starting this series:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Advanced Decorators or Generators: We’ll keep things simple.&lt;/li&gt;
&lt;li&gt;Web Frameworks: No Django or FastAPI knowledge is required.&lt;/li&gt;
&lt;li&gt;Database Management: We won’t be touching SQL or NoSQL in the beginning.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Our focus is strictly on &lt;strong&gt;Core Python OOP&lt;/strong&gt; step by step.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup &amp;amp; Environment
&lt;/h2&gt;

&lt;p&gt;Before we write our first class, make sure your environment is ready:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Python Installed: Ensure you have Python 3.x on your machine.&lt;/li&gt;
&lt;li&gt;Terminal Access: You should be comfortable running a script via &lt;code&gt;python&lt;/code&gt; &lt;code&gt;filename.py&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;Code Editor: Use whatever you like (VS Code, PyCharm, or even a simple text editor). No complex setup or heavy IDE configuration is required.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Run this to check your Python version&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;sys&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Python version&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//Should be 3.7 or higher&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to Master This Series
&lt;/h2&gt;

&lt;p&gt;To truly "level up" your skills, don't just be a spectator. Engage with the content:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Follow the Sequence: Concepts build on each other. Don't skip ahead!&lt;/li&gt;
&lt;li&gt;The "Type-Along" Rule: Never just read the code. Type it out. Muscle memory is a real thing in programming.&lt;/li&gt;
&lt;li&gt;Break Things: Change a value, delete a colon, or rename a variable. Seeing how the code breaks is the fastest way to learn how to fix it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Resources &amp;amp; Practice
&lt;/h2&gt;

&lt;p&gt;We believe in Hands-on Learning. To support you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All code examples are available on our GitHub repository.&lt;/li&gt;
&lt;li&gt;Links to the code are provided in our YouTube series descriptions.&lt;/li&gt;
&lt;li&gt;Clone it, fork it, or copy it just make sure you practice it.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final Note
&lt;/h2&gt;

&lt;p&gt;Object Oriented Programming isn't just a syntax change; it’s a mindset shift. It will help you write cleaner, reusable, and more professional code. Stay consistent, keep practicing, and don't be afraid to ask questions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Stay connected with hasabTech for more information:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://hasab.tech/" rel="noopener noreferrer"&gt;Website&lt;/a&gt; | &lt;a href="https://web.facebook.com/hasabTech" rel="noopener noreferrer"&gt;Facebook&lt;/a&gt; | &lt;a href="https://www.linkedin.com/company/hasabtech" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; | &lt;a href="https://www.youtube.com/@hasabTech" rel="noopener noreferrer"&gt;YouTube&lt;/a&gt; | &lt;a href="https://x.com/hasabTech" rel="noopener noreferrer"&gt;X (Twitter)&lt;/a&gt; | &lt;a href="https://www.tiktok.com/@hasabtech" rel="noopener noreferrer"&gt;TikTok&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>webdev</category>
      <category>coding</category>
    </item>
    <item>
      <title>What Is Object Oriented Programming (OOP) in Python?</title>
      <dc:creator>Shameel Uddin</dc:creator>
      <pubDate>Thu, 22 Jan 2026 11:48:06 +0000</pubDate>
      <link>https://dev.to/shameel/what-is-object-oriented-programming-oop-in-python-34ki</link>
      <guid>https://dev.to/shameel/what-is-object-oriented-programming-oop-in-python-34ki</guid>
      <description>&lt;p&gt;Object Oriented Programming (OOP) in Python is a programming approach where software is built using objects that represent real world entities. Each object is created from a class which acts as a blueprint defining its data (attributes) and behavior (methods).&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Do We Need Object Oriented Programming?
&lt;/h2&gt;

&lt;p&gt;In the early days of programming, developers wrote code line by line variables and functions were all here and there. Data was scattered across the program.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fj7nw4tft4uc4dbrnl69f.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fj7nw4tft4uc4dbrnl69f.gif" alt="Scattered Data Structure" width="760" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This worked for small scripts, but as applications grew larger (such as social media platforms) this spaghetti code became difficult to manage, maintain, and scale.&lt;/p&gt;

&lt;p&gt;Object Oriented Programming solves this problem by organizing code into structured, reusable components.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding OOP with a Real World Example
&lt;/h2&gt;

&lt;p&gt;Think about the creation of a human being. There is a basic biological blueprint that defines what makes a human:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Heart&lt;/li&gt;
&lt;li&gt;Brain&lt;/li&gt;
&lt;li&gt;Lungs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While individuals may differ in appearance, the core structure remains the same.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fkpj0ug4hrdg8h8n647e9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fkpj0ug4hrdg8h8n647e9.gif" alt="Blue Print" width="760" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In Python terms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The Blueprint = Class: A Class defines common properties and behaviours.&lt;/li&gt;
&lt;li&gt;The Human = Object: A real instance created from a class is called an Object.
Each object is unique, but all objects follow the same structure defined by the class.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Is a Class in Python?
&lt;/h2&gt;

&lt;p&gt;A class in Python is a template used to create objects. It generally defines two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Attributes: Data or properties (e.g., eye color, height).&lt;/li&gt;
&lt;li&gt;Methods: Functions that describe behavior (e.g., walking, talking).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Classes help developers write clean, organized, and reusable code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fycycmekbzwgr8kysr8z1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fycycmekbzwgr8kysr8z1.gif" alt="Blue Print" width="600" height="337"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is an Object in Python?
&lt;/h2&gt;

&lt;p&gt;An object in Python is a real instance created from a class. While a class is just a blueprint, an object is the actual thing that exists and can be used in a program.&lt;/p&gt;

&lt;p&gt;Each object:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Contains its own data (attributes)&lt;/li&gt;
&lt;li&gt;Can perform actions using methods defined in the class&lt;/li&gt;
&lt;li&gt;Is independent and unique, even if it is created from the same class&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, if a class represents a Human, then different people are objects of that class. They all share common features defined in the class, but their values (such as name, age, or height) can be different.&lt;/p&gt;

&lt;p&gt;In simple words:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Class = Blueprint&lt;/li&gt;
&lt;li&gt;Object = Real-world instance created from that blueprint&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Objects allow programs to work with real-world concepts, making code more organized, reusable, and easier to understand.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Is It Called Object Oriented Programming?
&lt;/h2&gt;

&lt;p&gt;It is called Object-Oriented Programming because programs are designed as a collection of organized, purposeful objects rather than just a loose list of instructions. This approach makes software easier to understand, maintain, and expand.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;OOP in Python uses objects to represent real-world entities.&lt;/li&gt;
&lt;li&gt;A Class is a blueprint or template defining attributes (data) and methods (behaviour).&lt;/li&gt;
&lt;li&gt;An Object is a real instance created from a class each object is unique but follows the same structure.&lt;/li&gt;
&lt;li&gt;Early programming worked for small scripts but as applications grew this line by line code turned into unmanageable spaghetti code&lt;/li&gt;
&lt;li&gt;OOP helps organize code, making it reusable, maintainable, and scalable.&lt;/li&gt;
&lt;li&gt;Real-world analogy: Humans are like objects created from a blueprint (class), with core features like a heart, brain, and lungs.&lt;/li&gt;
&lt;li&gt;Object Oriented Programming is called “object-oriented” because it treats programs as a collection of organized, purposeful objects.&lt;/li&gt;
&lt;li&gt;Benefits of OOP: Clean code, better structure, easy maintenance, and improved scalability.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What’s Next?
&lt;/h2&gt;

&lt;p&gt;Now that you understand the theory, are you ready to code? In the next blog, we will write our first Python Class and Object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Stay connected with hasabTech:&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Website: &lt;a href="https://hasab.tech/" rel="noopener noreferrer"&gt;https://hasab.tech/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Facebook: &lt;a href="https://www.facebook.com/hasabTech" rel="noopener noreferrer"&gt;https://www.facebook.com/hasabTech&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;LinkedIn: &lt;a href="https://www.linkedin.com/company/hasabtech" rel="noopener noreferrer"&gt;https://www.linkedin.com/company/hasabtech&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;X (Twitter): &lt;a href="https://x.com/hasabTec" rel="noopener noreferrer"&gt;https://x.com/hasabTec&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;YouTube: &lt;a href="https://youtube.com/@hasabTech" rel="noopener noreferrer"&gt;https://youtube.com/@hasabTech&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;TikTok: &lt;a href="https://tiktok.com/@hasabTech" rel="noopener noreferrer"&gt;https://tiktok.com/@hasabTech&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>webdev</category>
      <category>coding</category>
      <category>frontend</category>
    </item>
    <item>
      <title>How Does JavaScript Code Run?</title>
      <dc:creator>Shameel Uddin</dc:creator>
      <pubDate>Wed, 14 Jan 2026 14:27:25 +0000</pubDate>
      <link>https://dev.to/shameel/how-does-javascript-code-run-5b91</link>
      <guid>https://dev.to/shameel/how-does-javascript-code-run-5b91</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Have you ever wondered how JavaScript code actually runs?&lt;/p&gt;

&lt;p&gt;Does it execute on its own or is there a system working behind the scenes?&lt;/p&gt;

&lt;p&gt;In this blog we will clearly understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;how &lt;strong&gt;JavaScript code really runs&lt;/strong&gt;?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;what a JavaScript &lt;strong&gt;Runtime Environment&lt;/strong&gt; is?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The &lt;strong&gt;role of JavaScript Engine and it's types&lt;/strong&gt; (V8, SpiderMonkey, JavaScriptCore)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;how the &lt;strong&gt;JavaScript V8 Engine looks like from inside?&lt;/strong&gt; (Call Stack, Heap, Parser, JIT Compiler, Interpreter, and Garbage Collector)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This blog builds the foundation for understanding:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Call Stack in detail&lt;/li&gt;
&lt;li&gt;The working of JavaScript Engine, Execution Thread and Context&lt;/li&gt;
&lt;li&gt;The event loop&lt;/li&gt;
&lt;li&gt;Callback queue&lt;/li&gt;
&lt;li&gt;MicroTask queue&lt;/li&gt;
&lt;li&gt;Async JavaScript&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;which we will cover in our future blogs. In this blog, we focus on building a strong mental model of the JS engine itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Does JavaScript Run on Its Own?
&lt;/h2&gt;

&lt;p&gt;The simple answer is &lt;strong&gt;No&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;JavaScript code cannot do anything on its own unless it is executed inside a Runtime Environment.&lt;/p&gt;

&lt;p&gt;A runtime environment provides all the necessary tools, libraries, and features required for JavaScript to interact with the real world such as handling clicks, accessing files, or making network requests.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is a JavaScript Runtime Environment?
&lt;/h2&gt;

&lt;p&gt;JavaScript Runtime Environment is a system where JavaScript code lives and runs.&lt;/p&gt;

&lt;p&gt;You may have heard some popular runtime environments, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Web Browsers (Chrome, Firefox, Safari)&lt;/li&gt;
&lt;li&gt;Node.js&lt;/li&gt;
&lt;li&gt;Bun&lt;/li&gt;
&lt;li&gt;Deno&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of these provide an environment that allows JavaScript to execute.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Does a Runtime Environment Provide?
&lt;/h2&gt;

&lt;p&gt;Runtime environment offers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript Engine (the heart ♥️ of the runtime)&lt;/li&gt;
&lt;li&gt;Environment-specific APIs&lt;/li&gt;
&lt;li&gt;Event Loop, Callback Queue, and MicroTask Queue.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Together these components allow JavaScript to perform real-world tasks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F9zx2ep7qsc6wlelgs53e.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9zx2ep7qsc6wlelgs53e.gif" alt="JS Runtime Enviroment" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript Engine: The Heart of Runtime Environment
&lt;/h2&gt;

&lt;p&gt;JavaScript Engine is the core component responsible for executing JavaScript code.&lt;/p&gt;

&lt;p&gt;It is not something mysterious a JavaScript engine is simply a program that reads, compiles, and runs your code.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Popular JavaScript Engines&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Different environments use different engines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Chrome &amp;amp; Node.js → V8 Engine&lt;/li&gt;
&lt;li&gt;Firefox → SpiderMonkey&lt;/li&gt;
&lt;li&gt;Safari → JavaScriptCore&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fb4cno86kfo6b9tyb4xer.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fb4cno86kfo6b9tyb4xer.gif" alt="JavaScript Engine" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Environment Specific APIs
&lt;/h2&gt;

&lt;p&gt;Runtime environments also provide APIs which allow JavaScript to communicate with other software and systems. APIs depends on which environment they are in (like, Browser, Server, etc)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Browser APIs Examples&lt;/strong&gt;&lt;br&gt;
When JavaScript runs in a browser it gets access to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DOM (Document Object Model)&lt;/li&gt;
&lt;li&gt;Fetch API&lt;/li&gt;
&lt;li&gt;setTimeout and setInterval&lt;/li&gt;
&lt;li&gt;localStorage&lt;/li&gt;
&lt;li&gt;Event handling APIs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fywvbslabmgb9mr3yrty8.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fywvbslabmgb9mr3yrty8.gif" alt="Browser API" width="760" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Server-Side APIs (Node.js)
&lt;/h2&gt;

&lt;p&gt;In server environments like Node.js, JavaScript has access to APIs such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;File System (fs module)&lt;/li&gt;
&lt;li&gt;Networking&lt;/li&gt;
&lt;li&gt;Global objects&lt;/li&gt;
&lt;li&gt;Process management&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These APIs are not available in the browser which shows that APIs depend on the runtime environment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ft6uz95dueohmry9bw3cw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ft6uz95dueohmry9bw3cw.gif" alt="Server Side APIs" width="760" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Event Loop (Brief Overview)
&lt;/h2&gt;

&lt;p&gt;The runtime environment also contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Event Loop&lt;/li&gt;
&lt;li&gt;Callback Queue&lt;/li&gt;
&lt;li&gt;Microtask Queue&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These components handle asynchronous operations in JavaScript. We will discuss them in detail in upcoming blogs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Inside the JavaScript V8 Engine
&lt;/h2&gt;

&lt;p&gt;Now let’s take a deeper look at how a JavaScript V8 Engine (which is the most popular one) works internally.&lt;/p&gt;

&lt;h3&gt;
  
  
  Core Components of a JavaScript V8 Engine
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Heap Memory
&lt;/h4&gt;

&lt;p&gt;Heap Memory is an unstructured memory space used to store:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Objects&lt;/li&gt;
&lt;li&gt;Variables&lt;/li&gt;
&lt;li&gt;Functions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It is mainly used for dynamic memory allocation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Foy4b1fua5tfbcg9lsfdz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Foy4b1fua5tfbcg9lsfdz.gif" alt="Heap Memory" width="1920" height="1080"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Call Stack
&lt;/h4&gt;

&lt;p&gt;The Call Stack keeps track of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function calls&lt;/li&gt;
&lt;li&gt;Execution context&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It follows the Last In First Out (LIFO) principle. We’ll discuss this in detail in the next blog.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Ffkztecgsgn50hgvzxzvy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Ffkztecgsgn50hgvzxzvy.gif" alt="Call Stack" width="760" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional V8 Engine Components
&lt;/h2&gt;

&lt;p&gt;Modern JavaScript engines like V8 also include several optimisation and processing components:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Parser
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Reads JavaScript code&lt;/li&gt;
&lt;li&gt;Converts it into AST (Abstract Syntax Tree)&lt;/li&gt;
&lt;li&gt;AST represents code in a tree structured format&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  2. Interpreter
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Reads and executes code line by line&lt;/li&gt;
&lt;li&gt;Produces intermediate bytecode&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  3. JIT Compiler (Just-In-Time Compiler)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Converts byte code into machine readable code&lt;/li&gt;
&lt;li&gt;Improves performance by optimising frequently executed code&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  4. Garbage Collector
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Automatically cleans unused memory&lt;/li&gt;
&lt;li&gt;Removes objects and variables that are no longer in use&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F9oiuc7y5txkr0nyk8iax.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F9oiuc7y5txkr0nyk8iax.gif" alt="V8 Processing Components" width="760" height="427"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our next blog, we will understand the working and execution of JavaScript V8 Engine with the help of a simple code snippet example.&lt;/p&gt;

&lt;p&gt;Let’s quickly revise what we learnt in this blog.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The JavaScript language itself is limited until and unless it is inside of a Runtime environment.&lt;/li&gt;
&lt;li&gt;The runtime environment acts as a container that provides additional context, tools, and capabilities, enabling JavaScript to interact with browsers, servers, and outside world.&lt;/li&gt;
&lt;li&gt;The runtime environment comprises of JS Engine, environment specific APIs, Event Loop, Callback Queue, and MicroTask queue.&lt;/li&gt;
&lt;li&gt;Engine is the main working component of a runtime, which handles the execution.&lt;/li&gt;
&lt;li&gt;JavaScript V8 Engine comprises of a Heap Memory, Call Stack, Parser, JIT Compiler, Interpreter, and a Garbage Collector.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What’s Next?
&lt;/h2&gt;

&lt;p&gt;In our next blog, we will understand the execution and working of the JavaScript engine with the help of a simple code example, where we will explore the:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Call Stack and Heap Memory in action&lt;/li&gt;
&lt;li&gt;Execution Thread and Execution Context&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Stay connected with hasabTech:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Website: &lt;a href="https://hasab.tech/" rel="noopener noreferrer"&gt;https://hasab.tech/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Facebook: &lt;a href="https://www.facebook.com/hasabTech" rel="noopener noreferrer"&gt;https://www.facebook.com/hasabTech&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;LinkedIn: &lt;a href="https://www.linkedin.com/company/hasabtech" rel="noopener noreferrer"&gt;https://www.linkedin.com/company/hasabtech&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;X (Twitter): &lt;a href="https://x.com/hasabTec" rel="noopener noreferrer"&gt;https://x.com/hasabTec&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;YouTube: &lt;a href="https://youtube.com/@hasabTech" rel="noopener noreferrer"&gt;https://youtube.com/@hasabTech&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;TikTok: &lt;a href="https://tiktok.com/" rel="noopener noreferrer"&gt;https://tiktok.com/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Mastering Intermediate JavaScript</title>
      <dc:creator>Shameel Uddin</dc:creator>
      <pubDate>Wed, 07 Jan 2026 12:06:36 +0000</pubDate>
      <link>https://dev.to/shameel/mastering-intermediate-javascript-4ohb</link>
      <guid>https://dev.to/shameel/mastering-intermediate-javascript-4ohb</guid>
      <description>&lt;h2&gt;
  
  
  Understanding How JavaScript Really Works
&lt;/h2&gt;

&lt;p&gt;This Intermediate JavaScript Tutorial series is designed for developers who already understand JavaScript basics and want to go beyond syntax to understand how JavaScript actually works behind the scenes.&lt;/p&gt;

&lt;p&gt;If you’re comfortable with variables, loops, and functions, but struggle with asynchronous code, unexpected bugs, or confusing behavior, this series will help you build real JavaScript understanding instead of relying on guesswork.&lt;/p&gt;

&lt;p&gt;In this Intermediate JavaScript course, we explore the core concepts that power modern web applications, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; How JavaScript really runs (JavaScript engine and execution context)&lt;/li&gt;
&lt;li&gt; Call stack &amp;amp; memory heap&lt;/li&gt;
&lt;li&gt; Scopes in JavaScript&lt;/li&gt;
&lt;li&gt; Closures&lt;/li&gt;
&lt;li&gt; Hoisting (var vs let vs const)&lt;/li&gt;
&lt;li&gt; The this keyword in different contexts&lt;/li&gt;
&lt;li&gt; Prototype &amp;amp; prototype chain&lt;/li&gt;
&lt;li&gt; Event loop (microtasks vs macrotasks)&lt;/li&gt;
&lt;li&gt; Asynchronous JavaScript fundamentals&lt;/li&gt;
&lt;li&gt; Callbacks and Callback Hell&lt;/li&gt;
&lt;li&gt; Promises&lt;/li&gt;
&lt;li&gt; Async / await with real-world examples&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Many people say they “know JavaScript.”&lt;br&gt;
But knowing JavaScript syntax and understanding how JavaScript actually works behind the scenes are two very different things.&lt;/p&gt;

&lt;p&gt;If you can write variables, functions, loops, and basic logic, congratulations!&lt;br&gt;
You are no longer a beginner.&lt;/p&gt;

&lt;p&gt;However, becoming an Intermediate JavaScript developer means going much deeper than writing basic code.&lt;/p&gt;

&lt;p&gt;This blog introduces what Intermediate JavaScript really means and why it is a critical step in your web development journey.&lt;/p&gt;
&lt;h2&gt;
  
  
  Beginner vs Intermediate JavaScript: What’s the Difference?
&lt;/h2&gt;

&lt;p&gt;At the beginner level, you focus on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Writing correct syntax&lt;/li&gt;
&lt;li&gt;Making code “work”&lt;/li&gt;
&lt;li&gt;Using variables, loops, functions, and conditions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At the intermediate level, your focus shifts to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Understanding why your code behaves a certain way&lt;/li&gt;
&lt;li&gt;Knowing what happens internally in JavaScript&lt;/li&gt;
&lt;li&gt;Debugging issues without guessing&lt;/li&gt;
&lt;li&gt;Writing code that is predictable, scalable, and maintainable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is where real JavaScript learning begins.&lt;/p&gt;
&lt;h2&gt;
  
  
  What Does an Intermediate JavaScript Developer Know?
&lt;/h2&gt;

&lt;p&gt;An intermediate JavaScript developer is someone who:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Understands JavaScript behavior, not just syntax&lt;/li&gt;
&lt;li&gt;Can read and understand other developers’ code&lt;/li&gt;
&lt;li&gt;Writes clean, debuggable, and scalable code&lt;/li&gt;
&lt;li&gt;Knows how asynchronous JavaScript works&lt;/li&gt;
&lt;li&gt;Can confidently debug bugs and explain why they happened&lt;/li&gt;
&lt;li&gt;Is prepared to move into frameworks like React or backend tools like Node.js&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This level is about clarity and confidence, not just writing more code.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why Do Many Developers Get Stuck?
&lt;/h2&gt;

&lt;p&gt;Many developers move forward too fast.&lt;/p&gt;

&lt;p&gt;They jump into:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React&lt;/li&gt;
&lt;li&gt;Node.js&lt;/li&gt;
&lt;li&gt;Frameworks and libraries&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without fully understanding JavaScript core behavior&lt;/p&gt;

&lt;p&gt;This creates a dangerous gap.&lt;/p&gt;

&lt;p&gt;Later, this gap shows up as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Bugs they can’t explain&lt;/li&gt;
&lt;li&gt;Code they don’t fully trust&lt;/li&gt;
&lt;li&gt;Unexpected behavior in real projects&lt;/li&gt;
&lt;li&gt;Fear of debugging complex issues&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The problem is not frameworks. The problem is weak JavaScript foundations.&lt;/p&gt;
&lt;h2&gt;
  
  
  Why Understanding JavaScript Behavior Matters
&lt;/h2&gt;

&lt;p&gt;JavaScript behaves differently from many other programming languages, which is why simply knowing the syntax is not enough. At the intermediate level, developers begin to understand why bugs happen, why code behaves unexpectedly, and how JavaScript handles execution and asynchronous tasks internally. This deeper understanding allows developers to debug issues logically instead of relying on guesswork. Ultimately, this knowledge is what separates average developers from confident and professional JavaScript engineers.&lt;/p&gt;
&lt;h2&gt;
  
  
  About the hasabTech Intermediate JavaScript Series
&lt;/h2&gt;

&lt;p&gt;This is exactly why we designed the Intermediate JavaScript Series by hasabTech. The series focuses on understanding how JavaScript behaves, not just how it is written. It covers core concepts that developers usually skip, helps build strong fundamentals for real-world projects, and prepares learners for modern frameworks like React, backend tools such as Node.js, and advanced JavaScript development. Because knowing JavaScript is different from truly understanding JavaScript&lt;/p&gt;

&lt;p&gt;Watch the full video here&lt;/p&gt;

&lt;p&gt;

  &lt;iframe src="https://www.youtube.com/embed/xkSpjloM7iw"&gt;
  &lt;/iframe&gt;


&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Stay connected with hasabTech:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Website: &lt;a href="https://hasab.tech/" rel="noopener noreferrer"&gt;https://hasab.tech/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Facebook: &lt;a href="https://www.facebook.com/hasabTech" rel="noopener noreferrer"&gt;https://www.facebook.com/hasabTech&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;LinkedIn: &lt;a href="https://www.linkedin.com/company/hasabtech" rel="noopener noreferrer"&gt;https://www.linkedin.com/company/hasabtech&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;X (Twitter): &lt;a href="https://x.com/hasabTec" rel="noopener noreferrer"&gt;https://x.com/hasabTec&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;YouTube: &lt;a href="https://youtube.com/@hasabTech" rel="noopener noreferrer"&gt;https://youtube.com/@hasabTech&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;TikTok: &lt;a href="https://tiktok.com/@hasabtech" rel="noopener noreferrer"&gt;https://tiktok.com/@hasabtech&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>coding</category>
      <category>devops</category>
    </item>
    <item>
      <title>Career Accelerator Program to help learners become job ready</title>
      <dc:creator>Shameel Uddin</dc:creator>
      <pubDate>Mon, 05 Jan 2026 07:35:35 +0000</pubDate>
      <link>https://dev.to/shameel/career-accelerator-program-to-help-learners-become-job-ready-33p</link>
      <guid>https://dev.to/shameel/career-accelerator-program-to-help-learners-become-job-ready-33p</guid>
      <description>&lt;h1&gt;
  
  
  hasabTech Education Career Accelerator Program 🚀
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Submission for &lt;a href="https://dev.to/challenges/mux-2025-12-03"&gt;DEV's Worldwide Show and Tell Challenge Presented by Mux&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What We Built
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;hasabTech Career Accelerator Program&lt;/strong&gt; is a structured learning system designed to make tech learners job-ready developers. Unlike traditional tutorials, our program &lt;strong&gt;simulates a real-world software engineering environment&lt;/strong&gt;, where participants &lt;strong&gt;learn in sprint-based formats&lt;/strong&gt;, work individually on small tasks, collaborate in teams on real-world projects, track their progress, receive mentorship from industry developers, and grow their network by learning in a diverse and thriving tech community.&lt;/p&gt;

&lt;p&gt;By the end of the program, &lt;strong&gt;learners don’t just know code, they know how to work like a developer in a real software development environment&lt;/strong&gt;, which makes them &lt;strong&gt;job-ready&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Who this is for
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Beginners who know basic HTML/CSS/JS but feel stuck
&lt;/li&gt;
&lt;li&gt;Self-taught developers struggling to stay consistent
&lt;/li&gt;
&lt;li&gt;Students preparing for junior frontend or full-stack roles&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Pitch Video
&lt;/h2&gt;

&lt;p&gt;

&lt;iframe src="https://player.mux.com/W4I869VOQ8kIyOqghkAetdXV1u3Lz6IyF7Agh01C666g" width="710" height="399"&gt;
&lt;/iframe&gt;



  &lt;/p&gt;

&lt;h2&gt;
  
  
  Live Demo &amp;amp; Testing Access
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Live Dashboard
&lt;/h3&gt;

&lt;p&gt;Explore the working dashboard here:&lt;br&gt;&lt;br&gt;
👉 &lt;strong&gt;&lt;a href="https://accelerator.hasab.tech/" rel="noopener noreferrer"&gt;https://accelerator.hasab.tech/&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  How to Test the Platform
&lt;/h3&gt;

&lt;p&gt;You can explore the platform in &lt;strong&gt;two ways&lt;/strong&gt;, depending on what you want to evaluate:&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Option 1: New Learner Experience&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Sign up as a &lt;strong&gt;learner&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Complete the &lt;strong&gt;eligibility criteria&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Test:

&lt;ul&gt;
&lt;li&gt;Sprint flow&lt;/li&gt;
&lt;li&gt;Progress tracking&lt;/li&gt;
&lt;li&gt;Dashboard features&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Option 2: Enrolled Learner View (Quick Access)&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Use the following demo credentials to directly view an already enrolled student’s dashboard:&lt;br&gt;
Email: &lt;a href="mailto:test-email@hasab.tech"&gt;test-email@hasab.tech&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Password: A3f$9kLp&lt;/p&gt;




&lt;h3&gt;
  
  
  Tip for Reviewers
&lt;/h3&gt;

&lt;p&gt;Option 1 is recommended if you want to see our enrollment and eligibility criteria process. Option 2 is recommended if you want to &lt;strong&gt;quickly evaluate sprint structure, progress visualization, and learner analytics&lt;/strong&gt; without going through onboarding.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Story Behind It
&lt;/h2&gt;

&lt;p&gt;Most learners don’t struggle because they lack talent, they struggle because &lt;strong&gt;learning in tech is unstructured&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;We wanted to solve this by creating a &lt;strong&gt;guided system&lt;/strong&gt;: learners follow structured paths, complete practical sprints, track streaks, and get access to &lt;strong&gt;mentorship, internships, open source opportunities, resume polishing, and a community&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;It’s not just a course, it’s a &lt;strong&gt;career accelerator&lt;/strong&gt;.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Technical Highlights
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sprint-based learning:&lt;/strong&gt; Tasks are divided into practical, time-boxed sprints with clearly defined outcomes.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automated eligibility testing:&lt;/strong&gt; Learners submit JS tasks via GitHub pipelines, automatically validated before enrollment approval.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dashboard:&lt;/strong&gt; Learners can see their analytics and can submit daily and weekly updateds.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Gamification:&lt;/strong&gt; Learners can score points after submitting daily updates, weekly updates, completing tasks and completing sprints. If tasks and sprints are not completed on time, they get half the points.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Streaks:&lt;/strong&gt; Similar to snapchat, we introduced streak feature, so that students remain hooked in daily learning.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Leadersboard:&lt;/strong&gt; Learners can see others on leadersboard and can compete with others which reinforces healthy learning environment.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Discord Integration:&lt;/strong&gt; All the updates are sent to discord and everyone can see who submitted daily/weekly reports, completed tasks, etc which increases visibility throughout the community.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mentorship &amp;amp; Community:&lt;/strong&gt; 1:1 mentorship calls, real-world project opportunities, and a growing developer community on Discord.
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tech Stack:&lt;/strong&gt; React for frontend, Node.js backend, GitHub Actions for automation, and a scalable database for storing learner progress.
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;By submitting this, we hope to show how &lt;strong&gt;structured guidance + real-world practice + mentorship&lt;/strong&gt; can help learners transition from hobby coders to &lt;strong&gt;job-ready developers&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>devchallenge</category>
      <category>muxchallenge</category>
      <category>showandtell</category>
      <category>video</category>
    </item>
    <item>
      <title>How to Handle Custom Responses and Headers in NestJS with @Res</title>
      <dc:creator>Shameel Uddin</dc:creator>
      <pubDate>Mon, 28 Jul 2025 11:59:29 +0000</pubDate>
      <link>https://dev.to/shameel/how-to-handle-custom-responses-and-headers-in-nestjs-with-res-m76</link>
      <guid>https://dev.to/shameel/how-to-handle-custom-responses-and-headers-in-nestjs-with-res-m76</guid>
      <description>&lt;p&gt;In the previous blog, we discussed how to modify HTTP status codes and headers while sending responses to the client. In this post, we'll explore another approach: Modifying headers manually in a NestJS controller using @Res() — similar to how it's done in Express.js.&lt;/p&gt;

&lt;h2&gt;
  
  
  Express vs. NestJS Response Flow
&lt;/h2&gt;

&lt;p&gt;NestJS, by default, handles HTTP responses for you. This means you can simply return a value from your controller, and Nest will convert it into a response, but sometimes, you might want fine-grained control, like setting custom headers or using an existing Express-style library. In such cases, you can manually handle the response.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to Modify Headers with @Res()
&lt;/h2&gt;

&lt;p&gt;We already learned how to intercept Express request in our controller so we imported a request like&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;HttpCode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;HttpStatus&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can also import a response here like with full form &lt;code&gt;Request&lt;/code&gt; or short form &lt;code&gt;Res&lt;/code&gt;&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;HttpCode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;HttpStatus&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Header&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Res&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly we also import types for this particular Response from Express&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Create a &lt;code&gt;@Get&lt;/code&gt; Request&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="nc"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;   
&lt;span class="nc"&gt;FindAll&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Res&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;HttpStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;OK&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Understanding @Res, res, and Response
&lt;/h2&gt;

&lt;p&gt;In this code&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;HttpStatus&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Res&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;response&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ResponseController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nf"&gt;findAll&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Res&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;status&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;HttpStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;OK&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;@Res()&lt;/code&gt; → Decorator to inject the response object&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;response&lt;/code&gt; → A custom variable name (can also be res)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;Response&lt;/code&gt; → Type from express for better IntelliSense and typing&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So finally, we can modify our header manually and send response to our client.&lt;/p&gt;

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

&lt;p&gt;While NestJS encourages using its built-in response handling, the @Res decorator lets you leverage Express-style syntax for fine-grained control. Use this sparingly to avoid losing Nest’s powerful features.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Use manual response handling sparingly, as it bypasses features like interceptors, exception filters, and built-in decorator.&lt;/p&gt;

&lt;p&gt;That said, stay tuned for more advanced NestJS tips!&lt;/p&gt;

&lt;p&gt;Video explanation available here ↓&lt;/p&gt;

&lt;p&gt;  &lt;iframe src="https://www.youtube.com/embed/yX0di9NBfdg"&gt;
  &lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Follow me for more such content:&lt;br&gt;
YouTube: &lt;a href="https://www.youtube.com/@ShameelUddin123" rel="noopener noreferrer"&gt;https://www.youtube.com/@ShameelUddin123&lt;/a&gt;&lt;br&gt;
LinkedIn: &lt;a href="https://www.linkedin.com/in/shameeluddin/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/shameeluddin/&lt;/a&gt;&lt;br&gt;
Github: &lt;a href="https://github.com/Shameel123" rel="noopener noreferrer"&gt;https://github.com/Shameel123&lt;/a&gt;&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>node</category>
    </item>
  </channel>
</rss>
