<?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: Abdullah alshebel</title>
    <description>The latest articles on DEV Community by Abdullah alshebel (@aboahmd).</description>
    <link>https://dev.to/aboahmd</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%2F923609%2Ff933748c-6f85-407a-9213-123e1a2acf40.png</url>
      <title>DEV Community: Abdullah alshebel</title>
      <link>https://dev.to/aboahmd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aboahmd"/>
    <language>en</language>
    <item>
      <title>Understanding Python's Object Model: Mutable vs Immutable Objects</title>
      <dc:creator>Abdullah alshebel</dc:creator>
      <pubDate>Fri, 03 Oct 2025 10:00:33 +0000</pubDate>
      <link>https://dev.to/aboahmd/understanding-pythons-object-model-mutable-vs-immutable-objects-12m2</link>
      <guid>https://dev.to/aboahmd/understanding-pythons-object-model-mutable-vs-immutable-objects-12m2</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;In this post, I'll walk through key Python concepts I explored while learning more deeply about how &lt;strong&gt;objects, identity, and mutability&lt;/strong&gt; work. We'll examine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Python's &lt;code&gt;id()&lt;/code&gt; and &lt;code&gt;type()&lt;/code&gt; functions&lt;/li&gt;
&lt;li&gt;What makes objects mutable or immutable&lt;/li&gt;
&lt;li&gt;How this impacts program behavior&lt;/li&gt;
&lt;li&gt;Why it matters when passing arguments to functions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Throughout, I'll provide simple code examples to make each concept concrete and relatable for new and intermediate Python learners.&lt;/p&gt;




&lt;h2&gt;
  
  
  Understanding &lt;code&gt;id()&lt;/code&gt; and &lt;code&gt;type()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Every object in Python has a unique identity that you can inspect using the &lt;code&gt;id()&lt;/code&gt; function. You can also inspect the object's class with the &lt;code&gt;type()&lt;/code&gt; function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Example
&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;x&lt;/span&gt; &lt;span class="o"&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&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="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;        &lt;span class="c1"&gt;# Outputs object's unique identifier (memory address)
&lt;/span&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="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;      &lt;span class="c1"&gt;# &amp;lt;class 'list'&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;140234567890123
&amp;lt;class 'list'&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Identity vs Equality
&lt;/h3&gt;

&lt;p&gt;Even two variables holding identical contents may have different ids if they're different objects in memory. This helps distinguish &lt;strong&gt;identity&lt;/strong&gt; (&lt;code&gt;is&lt;/code&gt;) from &lt;strong&gt;equality&lt;/strong&gt; (&lt;code&gt;==&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;a&lt;/span&gt; &lt;span class="o"&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&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="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# True, they have equal contents
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# False, they're different objects
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Takeaway:&lt;/strong&gt; &lt;code&gt;==&lt;/code&gt; checks if values are equal, while &lt;code&gt;is&lt;/code&gt; checks if they're the same object in memory.&lt;/p&gt;




&lt;h2&gt;
  
  
  Mutable Objects
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;mutable object&lt;/strong&gt; can have its internal state changed after it is created. Common examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lists&lt;/li&gt;
&lt;li&gt;Dictionaries&lt;/li&gt;
&lt;li&gt;Sets&lt;/li&gt;
&lt;li&gt;Most classes with modifiable attributes&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example: Modifying a List
&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;numbers&lt;/span&gt; &lt;span class="o"&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&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="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# [1, 2, 3, 4]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Identity Persists After Modification
&lt;/h3&gt;

&lt;p&gt;Changing the contents doesn't change the identity:&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;before&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;after&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&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="n"&gt;before&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;after&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;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# [1, 2, 3, 4, 5]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  Immutable Objects
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Immutable objects&lt;/strong&gt; cannot be changed in place after they're created. These include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Integers&lt;/li&gt;
&lt;li&gt;Floats&lt;/li&gt;
&lt;li&gt;Strings&lt;/li&gt;
&lt;li&gt;Tuples&lt;/li&gt;
&lt;li&gt;Frozensets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Any 'modification' creates a &lt;strong&gt;new object&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example: Integers
&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;              &lt;span class="c1"&gt;# This rebinds x to a new object
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;        &lt;span class="c1"&gt;# The id is different now
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;140734567823456
140734567823488  # Different id!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example: Strings
&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;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt; world&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;        &lt;span class="c1"&gt;# New object, new id
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;            &lt;span class="c1"&gt;# hello world
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Why Does It Matter? Python's Treatment of Mutable vs Immutable Objects
&lt;/h2&gt;

&lt;p&gt;Understanding mutability influences everything from &lt;strong&gt;performance&lt;/strong&gt; to &lt;strong&gt;subtle bugs&lt;/strong&gt;. Python stores small integers and short strings in a cache, so they may appear to share ids in some cases, but in general, identity and mutability are fundamental to how variables work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Differences
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Aspect&lt;/th&gt;
&lt;th&gt;Mutable Objects&lt;/th&gt;
&lt;th&gt;Immutable Objects&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Can be changed in-place?&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Identity after modification&lt;/td&gt;
&lt;td&gt;Same&lt;/td&gt;
&lt;td&gt;Different&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Safe to share references?&lt;/td&gt;
&lt;td&gt;⚠️ Risky&lt;/td&gt;
&lt;td&gt;✅ Safe&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Examples&lt;/td&gt;
&lt;td&gt;list, dict, set&lt;/td&gt;
&lt;td&gt;int, str, tuple&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Implications
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Mutable objects:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Can be changed via various methods&lt;/li&gt;
&lt;li&gt;If two variables reference the same object, changes by one variable affect the other&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Immutable objects:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Safe to share&lt;/li&gt;
&lt;li&gt;Reassigning a variable to a new value doesn't alter the original object&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example Comparison
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Mutable example
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;modify_list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lst&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;lst&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;99&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;modify_list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&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="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# [1, 2, 3, 99], because lists are mutable
&lt;/span&gt;
&lt;span class="c1"&gt;# Immutable example
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;modify_int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="nf"&gt;modify_int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&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="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# 5, unchanged
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;






&lt;h2&gt;
  
  
  How Arguments Are Passed to Functions
&lt;/h2&gt;

&lt;p&gt;Python's argument passing is often summarized as &lt;strong&gt;"assignment by object reference"&lt;/strong&gt; or &lt;strong&gt;"pass by object reference."&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What This Means
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;For &lt;strong&gt;mutable types&lt;/strong&gt;, functions can change the content of the argument outside the function&lt;/li&gt;
&lt;li&gt;For &lt;strong&gt;immutable types&lt;/strong&gt;, any operation that looks like a modification (e.g., &lt;code&gt;x += 1&lt;/code&gt;) instead creates a new object and rebinds the local variable&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  More Examples
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Mutable Example (List)
&lt;/h4&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;add_element&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mylist&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;mylist&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="s"&gt;new&lt;/span&gt;&lt;span class="sh"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;lst&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nf"&gt;add_element&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lst&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="n"&gt;lst&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# [0, 'new']
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt; &lt;code&gt;[0, 'new']&lt;/code&gt; ✅ The original list was modified!&lt;/p&gt;

&lt;h4&gt;
  
  
  Immutable Example (Integer)
&lt;/h4&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;add_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&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;Inside function: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;num&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="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="nf"&gt;add_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&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;Outside function: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;n&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;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Inside function: 11
Outside function: 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;❌ The original integer was NOT modified!&lt;/p&gt;

&lt;h3&gt;
  
  
  Why This Matters
&lt;/h3&gt;

&lt;p&gt;This behavior is &lt;strong&gt;crucial&lt;/strong&gt; to avoid unexpected side effects or bugs, especially when working with functions that handle lists or dictionaries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Best Practices:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🔍 Be aware of which types are mutable&lt;/li&gt;
&lt;li&gt;📝 Document if your function modifies arguments&lt;/li&gt;
&lt;li&gt;🛡️ Use &lt;code&gt;.copy()&lt;/code&gt; if you need to avoid modifying the original&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Advanced Tasks: Exploring Object Internals
&lt;/h2&gt;

&lt;p&gt;While digging deeper, I explored several advanced concepts:&lt;/p&gt;

&lt;h3&gt;
  
  
  Topics Covered
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;✅ Inspecting memory addresses of objects&lt;/li&gt;
&lt;li&gt;✅ Testing Python's integer caching (small integers cached for efficiency)&lt;/li&gt;
&lt;li&gt;✅ Careful copying: &lt;code&gt;list.copy()&lt;/code&gt; vs slicing vs simple assignment&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Copying Lists: Three Approaches
&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;a&lt;/span&gt; &lt;span class="o"&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;# Approach 1: Simple assignment (both refer to same object)
&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&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;a: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&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;# [1, 2, 3, 4] - a was affected!
&lt;/span&gt;
&lt;span class="c1"&gt;# Approach 2: Using .copy() (creates separate object)
&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&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;a: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&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;# [1, 2, 3, 4] - unchanged
&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;c: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;c&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;# [1, 2, 3, 4, 5]
&lt;/span&gt;
&lt;span class="c1"&gt;# Approach 3: Using slicing (also creates separate object)
&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;[:]&lt;/span&gt;
&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&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;a: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&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;# [1, 2, 3, 4] - still unchanged
&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;d: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;d&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;# [1, 2, 3, 4, 6]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Integer Caching Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Small integers are cached
&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;256&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;256&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# True - same object!
&lt;/span&gt;
&lt;span class="c1"&gt;# Large integers are not
&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;
&lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# False - different objects
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Key Lessons
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Assignment doesn't copy&lt;/strong&gt; - it creates another reference&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Use &lt;code&gt;.copy()&lt;/code&gt; or slicing&lt;/strong&gt; to create independent copies&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Python caches small integers&lt;/strong&gt; (-5 to 256) for efficiency&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Understanding object identity&lt;/strong&gt; helps debug confusing behavior&lt;/li&gt;
&lt;/ol&gt;




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

&lt;p&gt;Understanding Python's object model, especially the difference between &lt;strong&gt;mutable&lt;/strong&gt; and &lt;strong&gt;immutable&lt;/strong&gt; objects, is foundational for writing reliable, bug-free code. It impacts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🔧 Function arguments and side effects&lt;/li&gt;
&lt;li&gt;🎯 Variable assignment behavior&lt;/li&gt;
&lt;li&gt;📦 Copying data structures safely&lt;/li&gt;
&lt;li&gt;⚡ Performance optimizations&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Summary Table
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Key Point&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;id()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns unique object identifier&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;type()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Returns object's class/type&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mutable&lt;/td&gt;
&lt;td&gt;Can be modified in-place (list, dict, set)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Immutable&lt;/td&gt;
&lt;td&gt;Cannot be modified (int, str, tuple)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Function Args&lt;/td&gt;
&lt;td&gt;Passed by object reference&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Copying&lt;/td&gt;
&lt;td&gt;Use &lt;code&gt;.copy()&lt;/code&gt; or &lt;code&gt;[:]&lt;/code&gt; for independent copies&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Practicing these patterns and observing object identities has fundamentally changed my perspective on how Python "really" works under the hood. I hope this guide helps you build a stronger mental model too! 🐍✨&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
    </item>
    <item>
      <title>🐚 What Happens When You Type ls *.c in the Terminal?</title>
      <dc:creator>Abdullah alshebel</dc:creator>
      <pubDate>Wed, 14 May 2025 17:58:31 +0000</pubDate>
      <link>https://dev.to/aboahmd/what-happens-when-you-type-ls-c-in-the-terminal-2njo</link>
      <guid>https://dev.to/aboahmd/what-happens-when-you-type-ls-c-in-the-terminal-2njo</guid>
      <description>&lt;p&gt;If you’re new to the command line, you might be scratching your head when someone types something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What does that even mean? What is &lt;code&gt;ls&lt;/code&gt;? What’s that little star doing? And why does it matter?&lt;/p&gt;

&lt;p&gt;Let’s walk through &lt;strong&gt;step-by-step&lt;/strong&gt; what’s happening &lt;strong&gt;behind the scenes&lt;/strong&gt;—like a detective solving a mystery. 🕵️‍♂️🔍&lt;/p&gt;




&lt;h2&gt;
  
  
  🎬 Scene 1: The Shell Enters the Chat
&lt;/h2&gt;

&lt;p&gt;Before anything runs, your &lt;strong&gt;shell&lt;/strong&gt; (like &lt;code&gt;bash&lt;/code&gt;, &lt;code&gt;zsh&lt;/code&gt;, etc.) is the real MVP. It’s the program that reads your command and makes sense of it.&lt;/p&gt;

&lt;p&gt;When you type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;shell&lt;/strong&gt;, &lt;em&gt;not&lt;/em&gt; the &lt;code&gt;ls&lt;/code&gt; command, is the one who notices the asterisk (&lt;code&gt;*&lt;/code&gt;) and goes, “Aha! That’s a wildcard!”&lt;/p&gt;




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

&lt;h3&gt;
  
  
  🪄 Step 1: Wildcard Expansion (aka Globbing)
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;*&lt;/code&gt; is a &lt;strong&gt;wildcard&lt;/strong&gt;. It means “match anything.”&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;*.c&lt;/code&gt; means: "Match all files that end in &lt;code&gt;.c&lt;/code&gt;"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, if your directory has:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;main.c
test.c
script.py
notes.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The shell will expand &lt;code&gt;*.c&lt;/code&gt; into:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;main.c test.c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ &lt;strong&gt;Key point&lt;/strong&gt;: The shell &lt;strong&gt;replaces&lt;/strong&gt; &lt;code&gt;*.c&lt;/code&gt; with a list of matching filenames &lt;em&gt;before&lt;/em&gt; it runs &lt;code&gt;ls&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  🧮 Step 2: Executing the &lt;code&gt;ls&lt;/code&gt; Command
&lt;/h3&gt;

&lt;p&gt;Now the shell runs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls &lt;/span&gt;main.c test.c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells the &lt;code&gt;ls&lt;/code&gt; command to list info about those specific files.&lt;/p&gt;

&lt;p&gt;So the terminal might output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;main.c  test.c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;💡 If you want detailed info (like file sizes), you could do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which expands and runs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="nt"&gt;-l&lt;/span&gt; main.c test.c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🚨 What If There Are No &lt;code&gt;.c&lt;/code&gt; Files?
&lt;/h2&gt;

&lt;p&gt;If there are &lt;strong&gt;no files ending in &lt;code&gt;.c&lt;/code&gt;&lt;/strong&gt;, the shell might just pass the literal &lt;code&gt;*.c&lt;/code&gt; to &lt;code&gt;ls&lt;/code&gt;, depending on your shell settings.&lt;/p&gt;

&lt;p&gt;You’ll then see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ls: cannot access '*.c': No such file or directory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;💥 Boom. Shell said, “I couldn’t find anything, so I left it as-is.”&lt;/p&gt;




&lt;h2&gt;
  
  
  🛠️ Real World Examples
&lt;/h2&gt;

&lt;h3&gt;
  
  
  ✅ Example 1: List all &lt;code&gt;.c&lt;/code&gt; files
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;main.c  utils.c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ✅ Example 2: Combine with other wildcards
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;&lt;span class="nb"&gt;test&lt;/span&gt;&lt;span class="k"&gt;*&lt;/span&gt;.c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Matches files like:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;unit_test.c  test_cases.c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  ⚠️ Example 3: Use quotes (prevents wildcard expansion!)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;ls&lt;/span&gt; &lt;span class="s2"&gt;"*.c"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is &lt;strong&gt;not&lt;/strong&gt; the same. With quotes, the &lt;code&gt;*&lt;/code&gt; is &lt;strong&gt;not expanded&lt;/strong&gt;. It looks for a file literally named &lt;code&gt;*.c&lt;/code&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 Bonus Tip: Globbing Is Everywhere
&lt;/h2&gt;

&lt;p&gt;Wildcards (&lt;code&gt;*&lt;/code&gt;, &lt;code&gt;?&lt;/code&gt;, etc.) work with lots of commands, not just &lt;code&gt;ls&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cp&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.txt backup/
&lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.log
&lt;span class="nb"&gt;cat &lt;/span&gt;data_??.csv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They’re part of &lt;strong&gt;shell globbing&lt;/strong&gt;, and it’s powerful once you get used to it! 💪&lt;/p&gt;




&lt;h2&gt;
  
  
  🧾 TL;DR
&lt;/h2&gt;

&lt;p&gt;When you type &lt;code&gt;ls *.c&lt;/code&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;strong&gt;shell&lt;/strong&gt; sees the &lt;code&gt;*&lt;/code&gt; and expands it to match filenames.&lt;/li&gt;
&lt;li&gt;It runs &lt;code&gt;ls&lt;/code&gt; with those filenames as arguments.&lt;/li&gt;
&lt;li&gt;You see the list of files that match &lt;code&gt;*.c&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And that’s it! 🎉 You're officially smarter than 80% of first-year CS students. Okay, maybe 60%. 😉&lt;/p&gt;

</description>
    </item>
    <item>
      <title>What happens when you type gcc main.c</title>
      <dc:creator>Abdullah alshebel</dc:creator>
      <pubDate>Thu, 01 May 2025 09:38:23 +0000</pubDate>
      <link>https://dev.to/aboahmd/what-happens-when-you-type-gcc-mainc-h17</link>
      <guid>https://dev.to/aboahmd/what-happens-when-you-type-gcc-mainc-h17</guid>
      <description>&lt;p&gt;&lt;strong&gt;GCC&lt;/strong&gt; (GNU Compiler Collection) is the workhorse that turns your C code into lightning-fast machine code.&lt;br&gt;
 In this article, we’ll explore why C uses a compiler, how compiling differs from interpreting, the landscape of other popular compilers, and then walk through each of GCC’s four stages—preprocessing, compiling to assembly, assembling to object code, and linking into an executable. 🚀&lt;/p&gt;


&lt;h2&gt;
  
  
  Why Do We Compile C at All? 🤔
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Performance:&lt;br&gt;
Compiled languages (like C) are translated into machine code ahead of time, so the CPU can execute them directly. This gives you blazing speed—crucial for system software, games, and real-time applications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Portability:&lt;br&gt;
The same C source can be compiled on Windows, Linux, macOS, or even embedded controllers. The compiler handles the platform differences.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Error Checking &amp;amp; Optimization:&lt;br&gt;
The compiler can detect syntax and type errors before you even run your program. It also performs powerful optimizations (inlining, loop unrolling, dead-code elimination) to squeeze out extra performance.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By contrast, an interpreter (e.g., Python’s python script.py) reads your code line by line at runtime, which is more flexible for scripting but generally much slower.&lt;/p&gt;


&lt;h2&gt;
  
  
  Compiler vs. Interpreter: The TL;DR 🔄
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Aspect&lt;/th&gt;
&lt;th&gt;Compiler (C, Rust)&lt;/th&gt;
&lt;th&gt;Interpreter (Python, Ruby)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Translation&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Ahead-of-time → machine code&lt;/td&gt;
&lt;td&gt;On-the-fly → virtual machine or direct execution&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Speed&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Fast at runtime&lt;/td&gt;
&lt;td&gt;Slower (per-line overhead)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Error Timing&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Catches many errors pre-run&lt;/td&gt;
&lt;td&gt;Errors only show when that line executes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Distribution&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Distribute binaries&lt;/td&gt;
&lt;td&gt;Distribute source (requires runtime)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;


&lt;h2&gt;
  
  
  Why GCC? And What Else Is Out There? 🌐
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;GCC (GNU Compiler Collection)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Mature, battle-tested since the late 1980s&lt;/li&gt;
&lt;li&gt;
Supports C, C++, Fortran, Ada, Go, and more
-
Highly portable (runs on hundreds of architectures)&lt;/li&gt;
&lt;li&gt;
Rich optimization flags (-O2, -O3, -Ofast, -march=…)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Clang/LLVM&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Modular, reusable libraries (LLVM IR)&lt;/li&gt;
&lt;li&gt;
Very fast compile times and user-friendly diagnostics&lt;/li&gt;
&lt;li&gt;
Used as the basis for Apple’s toolchain &lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;MSVC (Microsoft Visual C++)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Dominant on Windows for building native apps&lt;/li&gt;
&lt;li&gt;
Tight IDE integration and Windows SDK support&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;TinyCC (tcc)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
Super-lightweight, great for quick experimentation&lt;/li&gt;
&lt;li&gt;
Not production-grade for heavy optimization
---&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  The Four Stages of Compilation 🔍
&lt;/h2&gt;

&lt;p&gt;When you type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcc main.c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;GCC actually performs four distinct steps under the hood:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Preprocessing&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcc -E main.c -o main.i
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Includes&lt;br&gt;
Expands #include &amp;lt;...&amp;gt; and #include "..." by pasting in header contents.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Macros&lt;br&gt;
Expands #define macros.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Conditionals&lt;br&gt;
Evaluates #if, #ifdef, etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔎 Result: main.i — the pure C code the compiler sees, with no more preprocessor directives.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Compilation (C → Assembly)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcc -S main.i -o main.s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Syntax analysis&lt;br&gt;
Builds an abstract syntax tree (AST), ensures your code follows C grammar.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Semantic checks&lt;br&gt;
Type-checks, ensures you’re not, say, assigning a float* to an int without a cast.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Optimization&lt;br&gt;
Applies inlining, constant folding, loop transformations, etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Code generation&lt;br&gt;
Emits human-readable assembly for your target CPU.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔎 Result: main.s — the assembly instructions, like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.LC0:
    .string "Hello, world!"
movl    $.LC0, %edi
call    puts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Assembling (Assembly → Object Code)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcc -c main.s -o main.o
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Translation&lt;br&gt;
Turns assembly mnemonics into binary opcodes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Metadata&lt;br&gt;
Creates symbol tables (which functions and variables you define/export) and relocation entries.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🔎 Result: main.o — an object file containing machine code and metadata (but not yet a full program).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Linking (Object Code → Executable)&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcc main.o -o myapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Symbol resolution&lt;br&gt;
Matches your references (e.g., puts) to their definitions in libraries (e.g., libc).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Library inclusion&lt;br&gt;
Pulls in the necessary parts of the standard library or any other .a/.so you specify.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Relocation&lt;br&gt;
Adjusts addresses so that each piece of code and data sits at the correct memory location.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Final image&lt;br&gt;
Emits a standalone executable (myapp).&lt;br&gt;
🔎 Result: ./myapp runs your program!&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Putting It All Together with -v 🤓&lt;/strong&gt;&lt;br&gt;
Want to watch GCC orchestrate all these tools? Try:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gcc -v main.c -o myapp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You’ll see lines like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Using built-in specs.
COLLECT_GCC=gcc
…cc1 – the actual compiler for C…
/usr/bin/as – the assembler
/usr/bin/ld – the linker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>programming</category>
      <category>c</category>
    </item>
  </channel>
</rss>
