Python 6: Why Use Classes in Python? (Intro to OOP Logic)

🎯 Goal:

By the end of this lesson, you’ll understand:

  • What a class really is
  • Why classes are needed for real-world applications
  • When to use classes instead of plain functions and variables

🍪 1. What is a Class (Really)?

Think of a class as a blueprint or cookie cutter. It defines the structure and behavior of things you want to model — like a product, employee, or order in a retail store.

class Product:
    def __init__(self, name, price):
        self.name = name
        self.price = price

This is just the design. You still have to create actual products using it (called objects):

ring = Product("Diamond Ring", 2500)

Now you’ve made a real product — just like baking cookies from a mold.


🛠 2. The Problem Without Classes

Let’s say you’re selling a ring. Without using classes, here’s how you might track it:

product_name = "Diamond Ring"
product_price = 2500
product_stock = 10

def sell(quantity):
    global product_stock
    product_stock -= quantity

🧱 This works for one product. But what if you have 50?
You’d need:

  • product_name1, product_name2, product_name3, …
  • sell_product1(), sell_product2() … 😩

It becomes messy, repetitive, and hard to maintain.


✅ 3. Why Classes Are Better

Here’s how much cleaner and scalable it becomes:

class Product:
    def __init__(self, name, price, stock):
        self.name = name
        self.price = price
        self.stock = stock

    def sell(self, quantity):
        if quantity <= self.stock:
            self.stock -= quantity
            return f"Sold {quantity} x {self.name}"
        else:
            return f"Not enough stock for {self.name}"

Now you can do:

ring = Product("Diamond Ring", 2500, 10)
necklace = Product("Necklace", 1500, 5)
print(ring.sell(3))
print(necklace.sell(2))

🎉 No need for extra variables or duplicate logic. Every product has its own behavior and data.


🔁 4. When Should You Use Classes?

SituationUse a Class?Why?
One-time scriptFunctions are fine
Managing a store with products, employees, ordersReusable objects
Building an app or dashboardClean architecture
Prototyping something quickSimplicity matters

💡 5. OOP Powers You Unlock

Once you understand classes, you’ll soon be able to use:

  • Inheritance (e.g. Manager inherits from Employee)
  • Polymorphism (e.g. override behavior in different subclasses)
  • Encapsulation (keep your code tidy and modular)

🧪 6. Practice Time

Try these in your blog_env:

  • Create a Product class with name, price, and stock
  • Add a method called discount() that reduces the price
  • Create multiple product objects and print their updated prices

📘 Summary

  • A class is a template that describes a type of object
  • Objects created from classes are reusable and scalable
  • OOP makes your code clean, efficient, and real-world ready
  • It’s essential for any serious application like sales dashboards, inventory systems, or APIs

Comments

Leave a comment