Object-Oriented Programming in Python: A Real-Time Sales Data Example

PYTHON

5/7/20254 min read

In today’s data-driven world, businesses deal with real-time data streams—from customer orders to inventory updates. Managing this data efficiently requires clean, maintainable code. That's where Object-Oriented Programming (OOP) shines.

In this blog post, we’ll explore the fundamentals of OOP in Python using a sales system as our real-world example. Whether you're new to OOP or looking to solidify your understanding, this guide walks through classes, objects, inheritance, and encapsulation in a practical context.

📌 What is Object-Oriented Programming?

OOP is a programming paradigm that structures code using "objects"—instances of "classes" that bundle data and behavior.

Core concepts of OOP:

  • Class: A blueprint for creating objects.

  • Object: An instance of a class.

  • Encapsulation: Bundling data with methods that operate on that data.

  • Inheritance: A class can inherit attributes and methods from another class.

  • Polymorphism: Objects can take many forms depending on their class hierarchy.

Let’s bring this to life with a sales system example.

🔄 Scenario: Real-Time Sales Data

Imagine you work for an e-commerce platform. Every time a customer makes a purchase, the system records data such as:

  • Customer info

  • Products purchased

  • Payment details

  • Timestamp

Let’s model this using OOP.

🧱 Step 1: Define the Product Class

class Product:

def init(self, name, price, sku):

self.name = name

self.price = price

self.sku = sku

def str(self):

return f"{self.name} (${self.price})"

This class represents a product in your system. Each product has a name, price, and stock-keeping unit (SKU).

Explanation:
  • class Product:
    This defines a new class called Product, a template for creating product objects.

  • def init(self, name, price, sku):
    The constructor method. It’s called automatically when you create a new Product object. It takes in 3 parameters: name, price, and sku.

  • self.name = name, etc.
    These lines assign the input parameters to instance variables. This is how the object keeps track of its own data.

  • def str(self):
    This method is called when you print the object. It returns a string representation like Wireless Mouse ($29.99).

👥 Step 2: Define the Customer Class

class Customer:

def init(self, customer_id, name, email):

self.customer_id = customer_id

self.name = name

self.email = email

def str(self):

return f"{self.name} ({self.email})"

Here’s how we encapsulate customer data.

Explanation:
  • This class is similar to Product but represents a customer.

  • customer_id, name, and email are stored as instance variables.

  • The str() method lets us nicely print the customer as Alice Smith (alice@example.com).

🛒 Step 3: Define the Order Class

from datetime import datetime

class Order:

def init(self, order_id, customer):

self.order_id = order_id

self.customer = customer

self.products = []

self.timestamp = datetime.now()

def add_product(self, product):

self.products.append(product)

def get_total(self):

return sum(product.price for product in self.products)

def str(self):

product_list = ", ".join(str(p) for p in self.products)

return (f"Order #{self.order_id} by {self.customer.name}:\n"

f"Products: {product_list}\n"

f"Total: ${self.get_total():.2f}\n"

f"Timestamp: {self.timestamp}")

This class shows how OOP can help organize multiple data points into a coherent structure.

Explanation:
  • from datetime import datetime
    Imports Python's datetime module so we can timestamp orders.

  • class Order:
    Defines an order, which connects a customer to one or more products.

  • init() sets up the order:

    • order_id: Unique ID for the order.

    • customer: A Customer object.

    • products: An empty list that will store Product objects.

    • timestamp: When the order is created.

  • add_product(self, product)
    Appends a product to the products list.

  • get_total()
    Calculates the total price by summing up all product.price values.

  • str()
    Prepares a formatted summary of the order including:

    • Customer name

    • List of product names with prices

    • Total amount

    • Order timestamp

🔹 4. Simulating a Sale

# Create some products

p1 = Product("Wireless Mouse", 29.99, "MOUSE123")

p2 = Product("Keyboard", 49.99, "KEYB456")

# Create a customer

cust = Customer(1, "Alice Smith", "alice@example.com")

# Create an order

order = Order(101, cust)

order.add_product(p1)

order.add_product(p2)

# Print order summary

print(order)

Output:

Order #101 by Alice Smith:

Products: Wireless Mouse ($29.99), Keyboard ($49.99)

Total: $79.98

Timestamp: 2025-05-07 14:23:01.456789

Explanation:
  • p1 and p2 are two product objects with names, prices, and SKUs.

  • cust is a customer object representing Alice Smith.

  • order = Order(101, cust)
    Creates a new order with ID 101 for Alice.

  • order.add_product(p1)
    Adds the wireless mouse to the order.

  • order.add_product(p2)
    Adds the keyboard to the order.

  • print(order)
    Calls the str() method and prints a full summary of the order.

🚀 Benefits of Using OOP Here
  • Maintainability: Each class handles a specific responsibility.

  • Scalability: Add new features (like discounts, order tracking) with minimal changes.

  • Reusability: You can reuse and extend classes across multiple projects.

✍️ Conclusion

Using OOP to manage real-time sales data helps keep your code organized, readable, and scalable. By modeling real-world entities—like products, customers, and orders—you build systems that are closer to how we think about data.

Whether you're building a small app or a large e-commerce backend, OOP in Python is a great tool to have in your toolkit.

Kishore Babu Valluri

Senior Data Scientist | Freelance Consultant | AI/ML & GenAI Expert

With deep expertise in machine learning, artificial intelligence, and Generative AI, I work as a Senior Data Scientist, freelance consultant, and AI agent developer. I help businesses unlock value through intelligent automation, predictive modeling, and cutting-edge AI solutions.