My Journey into Python OOP: From Chef to Coder

·

4 min read

My Journey into Python OOP: From Chef to Coder

Hello there! If you're reading this, chances are you're either diving into Python OOP or feeling a bit lost—just like me when I started. I’m currently learning programming at a bootcamp, and let me tell you, it’s been a wild ride. Coming from a 20-years career as a chef, coding is an entirely new world for me. With three toddlers and only a couple of hours a day to study, there are times I feel completely overwhelmed. But you know what? There’s light at the end of the tunnel.

If I can do this, so can you! No matter how hard it gets, keep pushing forward. Progress may feel slow, but every small step counts. Every expert was once a beginner, and with persistence, you’ll get there too. Let’s take this journey together and explore Python’s Object-Oriented Programming (OOP) together!

Understanding Python OOP, The Power of Classes, Inheritance and more…..

In this blog, I will break down key OOP concepts in Python. Focusing on Inheritance, attributes, methods, property decorators . Let’s dive in with some hands-on code!

1 . Classes and Instances Methods

Everything in Python is an object and every object is an instance of a class. A class acts like a blueprint for creating objects.

Python

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

    def car_info(self):
        return f"{self.year} {self.brand} {self.model}"

# Creating instances
car1 = Car("Tesla", "Model S", 2022)
print(car1.car_info())  # Output: 2022 Tesla Model S

2 . Inheritance, Reusing Code Like a Pro

Inheritance allows a class to inherit attributes and methods from another class, avoiding redundancy.

Python

class ElectricCar(Car):  # Inherits from Car
    def __init__(self, brand, model, year, battery_size):
        super().__init__(brand, model, year)  # Calls parent constructor
        self.battery_size = battery_size

    def battery_info(self):
        return f"{self.car_info()} has a {self.battery_size} kWh battery."

# Creating an instance of ElectricCar
ecar = ElectricCar("Tesla", "Model 3", 2023, 75)
print(ecar.battery_info())

key Takeaway - super() allows us to call methods from the parent class without rewriting the entire constructor .

3 . Class Attributes vs. Instance Attributes

Class attributes are shared across all the instances, while instance attributes are unique to each object.

Python

class Vehicle:
    wheels = 4  # Class attribute (same for all instances)

    def __init__(self, brand):
        self.brand = brand  # Instance attribute (specific to each instance)

car = Vehicle("Toyota")
bike = Vehicle("Yamaha")

print(car.wheels, car.brand)  # Output: 4 Toyota
print(bike.wheels, bike.brand)  # Output: 4 Yamaha

key Takeaway - Changing wheels at the class level effects all instances .

4 . Class Methods working with the class itself

@classmethod operates on the class level rather than instances. it is commonly used for alternative constructors.

Python

class Person:
    species = "Homo sapiens"

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

    @classmethod
    def from_birth_year(cls, name, birth_year):
        return cls(name, 2024 - birth_year)  # cls refers to Person class

p = Person.from_birth_year("Alice", 1995)
print(p.name, p.age)  # Output: Alice 29

5 . Static Methods

@staticmethod is just like a regular method, but it belongs to a class for logical grouping .

Python

class Math:
    @staticmethod
    def add(x, y):
        return x + y

print(MathUtils.add(10, 5))  # Output: 15

key Takeaway - Static methods don’a access instances or class attributes.

6 . @property, Getters and Setters

In Python, @property is a way to control attribute access. it helps implement encapsulation by defining getter and setter methods.

Python

class Account:
    def __init__(self, balance):
        self._balance = balance  # _balance indicates internal use

    @property
    def balance(self):
        return self._balance  # Getter

    @balance.setter
    def balance(self, amount):
        if amount < 0:
            raise ValueError("Balance cannot be negative!")
        self._balance = amount  # Setter

acc = Account(1000)
print(acc.balance)  # Output: 1000

acc.balance = 1500  # Updating balance
print(acc.balance)  # Output: 1500

acc = Account(-500)
print(acc.balance)
 # Raises ValueError "Balance cannot be negative!"

key Takeaway - @property makes attribute access safer and more controlled.

Wrapping Up why OOP Matters in Python?

Using OOP in Python makes our code more organized (Encapsulation keeps data safe ), Easier to maintain (Inheritance reduces redundancy), More flexible( Class and static methods make reusability easier), @ property provides clean and controlled access. if you’re serious about mastering Python, understanding OOP is essential . Try using these concepts in projects and you’ll see how much cleaner and more efficient the code becomes.

Got any OOP tricks ? Drop them in the comments!

Happy coding !