<?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: sanin_mohd</title>
    <description>The latest articles on DEV Community by sanin_mohd (@saninmohd).</description>
    <link>https://dev.to/saninmohd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F800776%2F17ecd215-4955-4f51-8df6-3deba912ad2e.jpeg</url>
      <title>DEV Community: sanin_mohd</title>
      <link>https://dev.to/saninmohd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saninmohd"/>
    <language>en</language>
    <item>
      <title>Python !!! Mutable and Immutable objects</title>
      <dc:creator>sanin_mohd</dc:creator>
      <pubDate>Sat, 05 Feb 2022 16:17:29 +0000</pubDate>
      <link>https://dev.to/saninmohd/python-mutable-and-immutable-objects-107e</link>
      <guid>https://dev.to/saninmohd/python-mutable-and-immutable-objects-107e</guid>
      <description>&lt;blockquote&gt;
&lt;h1&gt;
  
  
  Points To Remember
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;Objects are stored in memory&lt;/li&gt;
&lt;li&gt;Variable are used to refer object&lt;/li&gt;
&lt;li&gt;Int, Float, String, Tuple, Set, list etc are inbuilt classes &lt;/li&gt;
&lt;li&gt;Int object stores integer type data&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Example
&lt;/h1&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  x = int(1) # int class constructor
  x = 1 
  # Both expressions are same
  # x is a pointer and 1 is an object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GqhYnm10--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfo7zwac1nk755ma2azt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GqhYnm10--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfo7zwac1nk755ma2azt.png" alt="Image description" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Different types of objects in python
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Mutable objects&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;A mutable object is an object whose state can be modified &lt;br&gt;
      after it is created&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;List, dictionary, set and user-defined classes are mutable objects.&lt;/p&gt;

&lt;h1&gt;
  
  
  Example
&lt;/h1&gt;

&lt;p&gt;Let's create a list&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   x = [1,2,3,4,5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;now, we print address id of x pointing&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   print(x)
   print("Object address id x pointing : ",id(x))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Run Program And See Output&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[1, 2, 3, 4, 5]&lt;br&gt;
Object address id of x :  140169509393728&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;now, we change first element of the list i.e&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   x[0] = 9
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;now, we print address id of x pointing&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   print(x)
   print("Object address id of x : ",id(x))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Run Program And See Output&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[1, 2, 3, 4, 5]&lt;br&gt;
Object address id of x :  140169509393728&lt;br&gt;
[9, 2, 3, 4, 5]&lt;br&gt;
Object address id of x :  140169509393728&lt;/code&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Since list is mutable object, We can see that, before and after changing object, Both address ids are same._&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Immutable Objects&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;A immutable object is an object whose state can't be modified &lt;br&gt;
      after it is created&lt;br&gt;
Int, Float, String, Tuple, decimal, bool and range are Immutable objects&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Example
&lt;/h1&gt;

&lt;p&gt;Let's create an String object "Hello"&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; x = "Hello"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;"Hello" is an object of type string&lt;br&gt;
x is a pointer to the object "Hello"&lt;/p&gt;

&lt;p&gt;Now, let's see       &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; print(x[0]) #this code will print 0th index value "H"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Run Program And See Output &lt;br&gt;
&lt;code&gt;H&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, we change 0th index value "H" to "A"&lt;br&gt;
let's see what happens&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  x[0] = "A"     
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Run Program And See Output &lt;/p&gt;

&lt;p&gt;&lt;code&gt;H&lt;br&gt;
Traceback (most recent call last):&lt;br&gt;
File "main.py", line 3, in &amp;lt;module&amp;gt;&lt;br&gt;
x[0]="A"&lt;br&gt;
TypeError: 'str' object does not support item assignment&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here programs throws an error ,Telling that TypeError: 'str' object does not support item assignment, which means string is Immutable.&lt;br&gt;
&lt;em&gt;&lt;strong&gt;we can only create and delete immutable objects, we can't change immutable objects&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  One tip to boost up your knowledge
&lt;/h2&gt;

&lt;p&gt;let's create another string object&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  x = "HAPPY"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;now, we print address id of x pointing&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   print(x)
   print("Object address id x pointing : ",id(x))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Run Program And See Output &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; HAPPY
 Object address id x pointing :  140210683580720
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;here, we change x i.e&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   x = "hAPPY"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;now, we print address id of x pointing&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   print(x)
   print("Object address id of x : ",id(x))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Run Program And See Output &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; HAPPY
 Object address id x pointing :  140210683580720
 hAPPY
 Object address id of x :  140210683580592
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;We can see that ,Both address ids are different, Which means here "HAPPY" is not changed to "hAPPY", But 2 separate string objects are created.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  One Last Tip
&lt;/h2&gt;

&lt;p&gt;let's create 2 variables with &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  x = "HAPPY"
  y = "HAPPY"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;now, we print address id of x,y pointing&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   print(x)
   print("Object address id x pointing : ",id(x))
   print(y)
   print("Object address id y pointing : ",id(y))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Run Program And See Output &lt;/p&gt;

&lt;p&gt;&lt;code&gt;HAPPY&lt;br&gt;
Object address id x pointing :  140210683580720&lt;br&gt;
HAPPY&lt;br&gt;
Object address id y pointing :  140210683580720&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here, both x and y address ids are same, which means both x,y are pointing towards the same object&lt;/p&gt;

&lt;blockquote&gt;
&lt;h1&gt;
  
  
  Points To keep in mind
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;Objects are stored in memory&lt;/li&gt;
&lt;li&gt;Variable are used to refer object&lt;/li&gt;
&lt;li&gt;Int, Float, String, Tuple, Set, list etc are inbuilt classes &lt;/li&gt;
&lt;li&gt;Int object stores integer type data&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

</description>
      <category>python</category>
      <category>oop</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
