<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[From Scripts to Systems]]></title><description><![CDATA[From Scripts to Systems]]></description><link>https://oluchiic.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 02 Sep 2026 13:56:38 GMT</lastBuildDate><atom:link href="https://oluchiic.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[From Scripts to Systems: My Week 2 Journey into OOP and Python’s Hidden Powers]]></title><description><![CDATA[After completing my first week of foundational Python, Week 2 was a deep dive into some of the most powerful concepts in programming: Object-Oriented Programming (OOP), Inheritance, Iterators, Polymorphism, and Scope.
This week was packed with theory...]]></description><link>https://oluchiic.hashnode.dev/from-scripts-to-systems-my-week-2-journey-into-oop-and-pythons-hidden-powers</link><guid isPermaLink="true">https://oluchiic.hashnode.dev/from-scripts-to-systems-my-week-2-journey-into-oop-and-pythons-hidden-powers</guid><category><![CDATA[Python]]></category><category><![CDATA[Inheritance  in python]]></category><category><![CDATA[encapsulation]]></category><category><![CDATA[oop]]></category><dc:creator><![CDATA[Cynthia Oluchukwu]]></dc:creator><pubDate>Mon, 22 Sep 2025 10:58:20 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1758538570168/2bd9db4f-c9ad-45fc-b703-802bbd2afaec.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>After completing my first week of foundational Python, Week 2 was a deep dive into some of the most powerful concepts in programming: Object-Oriented Programming (OOP), Inheritance, Iterators, Polymorphism, and Scope.</p>
<p>This week was packed with theory, practice, challenges, and lots of “what’s going wrong”moments. Let me share what I learned, what I built, the challenges I faced, and how I overcame them.</p>
<h2 id="heading-what-i-learned"><strong>What I Learned</strong></h2>
<h3 id="heading-1object-oriented-programming-oop"><strong>1.Object-Oriented Programming (OOP)</strong></h3>
<p>I understood how OOP helps organize code into classes and objects, making programs easier to structure and maintain.</p>
<ul>
<li><p>Classes are like blueprints.</p>
</li>
<li><p>Objects are instances of those blueprints.</p>
</li>
<li><p>Attributes and methods define their behavior.</p>
</li>
</ul>
<p>Example:</p>
<pre><code class="lang-python"><span class="hljs-comment">#define the Car class</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Car</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, brand, model</span>):</span>
        self.brand = brand
        self.model = model

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">display</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">f"Car brand: <span class="hljs-subst">{self.brand}</span>, Model: <span class="hljs-subst">{self.model}</span>"</span>)

<span class="hljs-comment">#instantiate two Car objects</span>
car1 = Car(<span class="hljs-string">"Toyota"</span>, <span class="hljs-string">"Corolla"</span>)
car2 = Car(<span class="hljs-string">"Mercedes"</span>, <span class="hljs-string">"Benz"</span>)

<span class="hljs-comment">#print their details</span>
car1.display()
car2.display()
</code></pre>
<p>In this example, we defined a Car class with two attributes: brand and model.</p>
<p>The <strong>init</strong> method is a constructor that initializes these attributes whenever a new Car object is created.</p>
<p>The display() method is used to print the details of the car in a clean format.</p>
<p>We then created two objects (car1 and car2) with different values and called their display() methods, which printed their respective details.</p>
<p>This demonstrates the basics of object instantiation (creating objects from a class) and method usage in Python OOP.</p>
<h3 id="heading-2inheritance"><strong>2.Inheritance</strong></h3>
<p>A class can inherit attributes and methods from another class making code reusable and extensible.</p>
<p>Example:</p>
<pre><code class="lang-python"><span class="hljs-comment">#base class</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name, sound</span>):</span>
        self.name = name
        self.sound = sound

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">make_sound</span>(<span class="hljs-params">self</span>):</span>
        print(self.sound)

<span class="hljs-comment">#subclass Dog</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Dog</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name</span>):</span>
        super().__init__(name, <span class="hljs-string">"Woof!"</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">make_sound</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Woof!"</span>)

<span class="hljs-comment">#subclass Cat</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Cat</span>(<span class="hljs-params">Animal</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, name</span>):</span>
        super().__init__(name, <span class="hljs-string">"Meow!"</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">make_sound</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Meow!"</span>)


<span class="hljs-comment">#create objects</span>
dog1 = Dog(<span class="hljs-string">"Brave"</span>)
cat1 = Cat(<span class="hljs-string">"Hover"</span>)

<span class="hljs-comment">#Calling method</span>
dog1.make_sound()
cat1.make_sound()
</code></pre>
<p>In this example, we start with a base class Animal that has a make_sound() method.</p>
<p>The subclasses Dog and Cat inherit from Animal but override the make_sound() method to provide their own unique sounds ("Woof!" and "Meow!").</p>
<p>When we create objects (dog1 and cat1) and call their make_sound() methods, Python executes the version defined in the subclass instead of the base class.</p>
<p>This is a demonstration of inheritance (classes reusing functionality from a parent class).</p>
<h3 id="heading-3iterators"><strong>3.Iterators</strong></h3>
<p>Iterators let us loop through custom objects using <strong>iter</strong>() and <strong>next</strong>().</p>
<p>Example:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CountDown</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span>(<span class="hljs-params">self, n</span>):</span>
        self.n = n

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__iter__</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">return</span> self

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__next__</span>(<span class="hljs-params">self</span>):</span>
        <span class="hljs-keyword">if</span> self.n &gt; <span class="hljs-number">0</span>:
            current = self.n
            self.n -= <span class="hljs-number">1</span>
            <span class="hljs-keyword">return</span> current
        <span class="hljs-keyword">else</span>:
            <span class="hljs-keyword">raise</span> StopIteration

<span class="hljs-comment"># Demonstration</span>
num = CountDown(<span class="hljs-number">10</span>)

<span class="hljs-keyword">for</span> number <span class="hljs-keyword">in</span> num:
    print(number)
</code></pre>
<p>In this example, we define a custom iterator class CountDown.</p>
<p>• The <strong>iter</strong>() method returns the iterator object itself (required for iteration).</p>
<p>• The <strong>next</strong>() method defines how the iteration progresses. It decreases the number n by 1 each time and returns the current value. Once n reaches 0, it raises a StopIteration exception to end the loop.</p>
<p>When we use a for loop on the CountDown(10) object, Python internally keeps calling <strong>next</strong>() until there are no more values to return.</p>
<p>This is a demonstration of how the iterator protocol works in Python making objects iterable just like lists, strings, or tuples.</p>
<h3 id="heading-4polymorphism"><strong>4.Polymorphism</strong></h3>
<p>Different classes can have methods with the same name, but behave differently.</p>
<pre><code class="lang-python"><span class="hljs-comment"># base class</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Bird</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">speak</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Chirp"</span>)

<span class="hljs-comment"># subclass Parrot</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Parrot</span>(<span class="hljs-params">Bird</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">speak</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Squawk"</span>)

<span class="hljs-comment"># subclass Crow</span>
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Crow</span>(<span class="hljs-params">Bird</span>):</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">speak</span>(<span class="hljs-params">self</span>):</span>
        print(<span class="hljs-string">"Caw"</span>)

<span class="hljs-comment"># function to make bird speak</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">make_bird_speak</span>(<span class="hljs-params">bird</span>):</span>
    bird.speak()

<span class="hljs-comment"># create objects</span>
parrot = Parrot()
crow = Crow()

<span class="hljs-comment"># function calls</span>
make_bird_speak(parrot)   <span class="hljs-comment"># Squawk</span>
make_bird_speak(crow)     <span class="hljs-comment"># Caw</span>
</code></pre>
<p>In this example, we start with a base class Bird that has a method speak().</p>
<p>• The subclasses Parrot and Crow override the speak() method to give their own unique sounds: "Squawk" for the parrot and "Caw" for the crow.</p>
<p>• The function make_bird_speak() accepts any Bird object and calls its speak() method, without needing to know whether it’s a parrot, crow, or any other bird.</p>
<p>This demonstrates polymorphism in Python the same method name (speak) behaves differently depending on the object that calls it.</p>
<h3 id="heading-5scope-global-local-nonlocal-built-in"><strong>5.Scope (Global, Local, Nonlocal, Built-in)</strong></h3>
<p>Variable visibility depends on where it is defined.</p>
<p>• Local&gt; inside a function.</p>
<p>•Global &gt; defined outside all functions.</p>
<p>•Nonlocal &gt;used inside nested functions.</p>
<p>•Built-in &gt;Python reserved names (like len, print).</p>
<pre><code class="lang-python">count = <span class="hljs-number">10</span>  <span class="hljs-comment"># global</span>

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">increment</span>():</span>
    count = <span class="hljs-number">5</span>  <span class="hljs-comment"># local</span>
    print(<span class="hljs-string">"Inside function:"</span>, count)

increment()
print(<span class="hljs-string">"Outside function:"</span>, count)
</code></pre>
<p>In this example, we defined a global variable count = 10. Inside the function increment(), we created another variable named count = 5.</p>
<p>• When we print count inside the function, Python uses the local variable (value 5).</p>
<p>• When we print count outside the function, Python uses the global variable (value 10).</p>
<p>This shows the difference between local scope (variables defined inside a function) and global scope (variables defined outside all functions).</p>
<h2 id="heading-what-i-built-coded"><strong>What I Built / Coded</strong></h2>
<p>• A library system with Books and Members (using inheritance and encapsulation).</p>
<p>• A Fibonacci iterator to generate numbers up to n.</p>
<p>• Custom iterators for even numbers and file reading.</p>
<p>• OOP examples showing polymorphism (Car vs Bicycle, Cat vs Dog).</p>
<h2 id="heading-challenges-i-faced"><strong>Challenges I Faced</strong></h2>
<ol>
<li><p>Indentation Errors – I often misplaced indentation inside classes and methods, which caused frustrating errors.<br /> Solution: I practiced writing cleaner code consistently and relied on my IDE to quickly spot and correct indentation issues.</p>
</li>
<li><p>Confusion with Scope – At some point, I accidentally overwrote built-in names (like len), which led to unexpected errors and confusion.<br /> Solution: I learned to avoid shadowing built-in functions and studied Python’s LEGB rule (Local → Enclosed → Global → Built-in) to understand how variable resolution works.</p>
</li>
<li><p>Iterator Logic – My first iterator didn’t behave as expected because it returned object references instead of actual values.<br /> Solution: I debugged step by step and realized I needed to properly return values inside the <strong>next</strong>() method instead of printing the object itself.</p>
</li>
</ol>
<p>Although it wasn’t easy and I had sleepless nights trying to finish my tasks, these challenges made me stronger. YouTube tutorials also helped me a great deal whenever I was stuck, and overcoming these difficulties gave me more confidence in OOP, inheritance, polymorphism, iterators, and Python as a whole.</p>
<h2 id="heading-my-perspective"><strong>My Perspective</strong></h2>
<p>This week really showed me why OOP matters in programming. Being able to write code that’s structured and reusable feels so much better than just piecing scripts together. Working with iterators and polymorphism stretched how I think about problems, and learning more about scope gave me more confidence whenever I ran into errors.</p>
<p>I’ve come to see that programming isn’t only about writing code it’s about solving problems, thinking in systems, and building something you can reuse and improve on.</p>
<p>I’m looking forward to the next few weeks, but this week gave me a solid foundation and made me feel like I’m starting to think like a real developer.</p>
<p>That’s my Week 2 recap. If you’ve learned OOP, Iterators, Polymorphism, or struggled with scope, I’d love to hear your experience too.</p>
]]></content:encoded></item></channel></rss>