Skip to main content

Command Palette

Search for a command to run...

🚀 My Dive into Object-Oriented Programming (OOP) in Python

Published
•3 min read•View as Markdown

I recently started learning Object-Oriented Programming (OOP) in Python, and it’s a powerful way to organize code. Instead of just writing a sequence of instructions, OOP lets you create objects that bundle data and behavior together — making your code more modular, reusable, and easier to understand.

Here’s a quick breakdown of what I learned, with simple examples.


🧱 What is OOP?

  • Class: A blueprint for creating objects. Think of it like a template.

  • Object: An instance of a class. It’s a specific thing created from the blueprint.

  • Method: A function defined inside a class that describes the behavior of objects.


✨ The Basics: Defining a Class and Creating Objects

Here’s an example of a simple Car class:

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

    def drive(self):
        print(f"The {self.year} {self.brand} {self.model} is driving")

    def stop(self):
        print("The car has stopped!")
  • The __init__ method is a constructor that runs when you create a new Car.

  • self refers to the current object.

  • drive and stop are methods that describe what a car can do.

Now, let’s create some cars:

my_car = Car("Tesla", "Model S", 2023)
your_car = Car("Toyota", "Corolla", 2022)

my_car.drive()
my_car.stop()
your_car.drive()
your_car.stop()

Output:

The 2023 Tesla Model S is driving
The car has stopped!
The 2022 Toyota Corolla is driving
The car has stopped!

🐶 Magic Methods: __init__ and __str__

Python has special methods (called magic methods) that let you customize how your objects behave.

For example, the __str__ method controls what happens when you print an object:

class Dog:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def bark(self):
        print("woof!")

    def __str__(self):
        return f"{self.name} is {self.age} years old"

my_dog = Dog("Buddy", 3)
print(my_dog)

Output:

Buddy is 3 years old

This way, printing my_dog shows a friendly description instead of a generic memory address.

Other magic methods include __len__, __add__, and many more that help objects behave like built-in Python types.


📚 Another Example: The Book Class

class Book:
    def __init__(self, title, author, pages):
        self.title = title
        self.author = author
        self.pages = pages

    def __str__(self):
        return f"'{self.title}' by {self.author} - {self.pages} pages"

book1 = Book("1984", "George Orwell", 328)
print(book1)

Output:

'1984' by George Orwell - 328 pages

🔄 Inheritance: Reusing Code with Parent and Child Classes

Inheritance lets you create new classes that reuse code from existing ones. This saves you from repeating yourself.

class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        print(f"{self.name} makes a sound!")

class Dog(Animal):
    def bark(self):
        print(f"{self.name} barks!")

class Cat(Animal):
    def meow(self):
        print(f"{self.name} meows")

Using these classes:

dog = Dog("Buddy")
dog.speak()  # inherited method
dog.bark()

cat = Cat("Whiskers")
cat.speak()
cat.meow()

Output:

Buddy makes a sound!
Buddy barks!
Whiskers makes a sound!
Whiskers meows

🎯 What I Took Away

  • OOP helps organize your code with real-world concepts.

  • Classes and objects let you model things with properties (attributes) and behaviors (methods).

  • Magic methods customize object behavior.

  • Inheritance allows you to build on existing classes without rewriting code.

More from this blog

My Python Journey

24 posts