Skip to main content

Command Palette

Search for a command to run...

# Week 2 – Cracking OOP in Python

Published
2 min read

This week was all about Object-Oriented Programming (OOP). At first, I struggled — inheritance, polymorphism, and iterators all felt abstract. But after lots of coding (and mistakes), things began to click. A big breakthrough came from Mr Winner’s Saturday class, which tied it all together.


Key Learnings

1. Classes & Objects

class Vehicle:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

    def display_info(self):
        print(f"Vehicle: {self.brand} {self.model}")


class Car(Vehicle):
    def __init__(self, brand, model, doors):
        super().__init__(brand, model)
        self.doors = doors

    def display_info(self):
        print(f"Car: {self.brand} | {self.model} | Doors: {self.doors}")

class ElectricCar(Car):
    def __init__(self, brand, model, doors, battery_capacity):
        super().__init__(brand, model, doors)
        self.battery_capacity = battery_capacity

    def display_info(self):
        print(f"Electric Car: {self.brand} {self.model} | Doors: {self.doors} | Battery: {self.battery_capacity}")

x = ElectricCar("Ferrari", "Model F", 2, 1000)
x.display_info()

Output: Electric Car: Ferrari Model F | Doors: 2 | Battery: 1000


2. Iterators

numbers = [10, 20, 30, 40, 50]
myiter = iter(numbers)
while True:
    try:
        number = next(myiter)
        print(number)
    except StopIteration:
        print('End of List')
        break

Output: 10 20 30 40 50 End of List


3. Polymorphism

class Bird:
    def speak(self):
        print("Chirp")

class Parrot(Bird):
    def speak(self):
        print("Squawk")

class Crow(Bird):
    def speak(self):
        print("Caw")

def make_bird_speak(bird):
    bird.speak()

#Test
parrot = Parrot()
crow = Crow()

make_bird_speak(parrot) 
make_bird_speak(crow)

Output: Squawk, Caw


4. Mini Library System

class Book: ...
class EBook(Book): ...
class PrintedBook(Book): ...
class Member: ...
class Library: ...

This project brought everything together: inheritance, encapsulation, and iterators, making OOP feel practical.


Reflection

  • Iterators are really powerful once you write your own.
  • Learned from mistakes like shadowing len() and handling some errors.
  • Inheritance and polymorphism are great concepts in OOP, and I understand better now
  • The Library project was tough, but it showed how OOP solves real problems.

Buzzing for Week 3


More from this blog

H

holat

7 posts