Classes and Objects#

Class = Blueprint | Object = Real customer/order Customer("Alice") = Instant business object

This = Foundation of Amazon/Google enterprise systems


🎯 Classes = Business Object Factory#

Business Thing

Class

Object Example

Method

Customer

Customer

Customer("Alice")

.lifetime_value()

Order

Order

Order(alice, 1200)

.process_payment()

Product

Product

Product("Laptop", 1200)

.calculate_discount()

Inventory

Inventory

Inventory()

.add_stock(50)


πŸš€ Step 1: YOUR First Class (Run this!)#

# CREATE YOUR FIRST BUSINESS CLASS
class Customer:
    """Customer object with business logic"""

    def __init__(self, name, initial_spend=0):
        self.name = name
        self.spend = initial_spend

    def add_purchase(self, amount):
        self.spend += amount
        print(f"βœ… {self.name} purchased ${amount:,}")

    def lifetime_value(self):
        """Business formula: spend * 1.5 multiplier"""
        return self.spend * 1.5

    def is_vip(self):
        return self.spend > 3000

# CREATE REAL BUSINESS OBJECTS
alice = Customer("Alice Johnson", 1200)
bob = Customer("Bob Smith", 5000)

# USE BUSINESS METHODS
alice.add_purchase(2800)
bob.add_purchase(1500)

print("πŸ‘₯ CUSTOMER SYSTEM:")
print(f"   Alice LTV: ${alice.lifetime_value():,.0f} {'⭐VIP' if alice.is_vip() else ''}")
print(f"   Bob LTV:   ${bob.lifetime_value():,.0f} {'⭐VIP' if bob.is_vip() else ''}")

Output:

βœ… Alice Johnson purchased $2,800
βœ… Bob Smith purchased $1,500
πŸ‘₯ CUSTOMER SYSTEM:
   Alice LTV: $6,000 ⭐VIP
   Bob LTV:   $9,750 ⭐VIP

πŸ”₯ Step 2: Multiple Objects = Business Ecosystem#

# FULL BUSINESS SYSTEM
class Product:
    def __init__(self, name, price):
        self.name = name
        self.price = price

    def apply_discount(self, percentage):
        self.price *= (1 - percentage/100)
        print(f"πŸ’° {self.name} discounted to ${self.price:,.0f}")

class Order:
    def __init__(self, customer, product, quantity=1):
        self.customer = customer
        self.product = product
        self.quantity = quantity
        self.total = product.price * quantity

    def process_order(self):
        self.customer.add_purchase(self.total)
        print(f"πŸ“¦ Order processed: {self.customer.name} bought {self.quantity} {self.product.name}")

# BUILD ECOSYSTEM
laptop = Product("MacBook Pro", 2500)
headphones = Product("AirPods", 200)

order1 = Order(alice, laptop, 1)
order2 = Order(bob, headphones, 2)

# REAL BUSINESS FLOW
laptop.apply_discount(10)
order1.process_order()
order2.process_order()

print(f"\nπŸ’Ό BUSINESS ECOSYSTEM:")
print(f"   Alice total spend: ${alice.spend:,.0f}")
print(f"   Bob total spend: ${bob.spend:,.0f}")

🧠 Step 3: Class Attributes = Shared Business Data#

class BankAccount:
    # SHARED BY ALL ACCOUNTS
    bank_name = "TechBank"
    interest_rate = 0.03

    def __init__(self, owner, initial_balance=0):
        self.owner = owner
        self.balance = initial_balance

    def deposit(self, amount):
        if amount > 0:
            self.balance += amount
            print(f"βœ… {self.owner}: +${amount:,} (Balance: ${self.balance:,.0f})")
        else:
            print("❌ Invalid deposit amount")

    def get_interest(self):
        return self.balance * BankAccount.interest_rate

# MULTIPLE ACCOUNTS SHARE BANK DATA
account1 = BankAccount("Alice", 10000)
account2 = BankAccount("Bob", 5000)

account1.deposit(2000)
account2.deposit(1500)

print(f"\n🏦 BANK SYSTEM:")
print(f"   {BankAccount.bank_name} Interest:")
print(f"   Alice: ${account1.get_interest():,.0f}")
print(f"   Bob:   ${account2.get_interest():,.0f}")

πŸ“‹ Class Anatomy Cheat Sheet#

Part

Code

Business Use

__init__

def __init__(self, name):

Create object

Instance attr

self.name = name

Per-object data

Class attr

ClassName.rate = 0.03

Shared data

Method

def lifetime_value(self):

Business logic

Self

self.spend

Current object

# PRO TIP: Self = "This customer/order"
# self.name β†’ This customer's name

πŸ† YOUR EXERCISE: Build YOUR Business System#

# MISSION: Create YOUR e-commerce system!

# 1. YOUR PRODUCT CLASS
class Product:
    def __init__(self, name, price):
        self.name = name
        self.price = price

    def apply_discount(self, percentage):
        self.price *= (1 - percentage / 100)
        print(f"πŸ’° {self.name}: ${self.price:,.0f}")

# 2. YOUR CUSTOMER CLASS
class Customer:
    def __init__(self, name, initial_spend=0):
        self.name = name
        self.spend = initial_spend

    def add_purchase(self, amount):
        self.spend += amount
        print(f"βœ… {self.name}: +${amount:,.0f}")

    def lifetime_value(self):
        return self.spend * 1.5

# 3. YOUR BUSINESS (Fill in YOUR data!)
your_customer = Customer("??? Smith", ???)  # YOUR customer
your_product = Product("??? Pro", ???)      # YOUR product

# 4. REAL BUSINESS FLOW
your_product.apply_discount(15)             # 15% off
your_customer.add_purchase(your_product.price)

print("\nπŸš€ YOUR E-COMMERCE SYSTEM:")
print(f"   Customer: {your_customer.name}")
print(f"   Product: {your_product.name} (${your_product.price:,.0f})")
print(f"   LTV: ${your_customer.lifetime_value():,.0f}")

Example to test:

your_customer = Customer("Alice Johnson", 1200)
your_product = Product("MacBook Pro", 2500)

YOUR MISSION:

  1. Create YOUR customer + product

  2. Run business flow

  3. Screenshot β†’ β€œI built e-commerce system!”


πŸŽ‰ What You Mastered#

Skill

Status

Business Power

Class creation

βœ…

Object factory

__init__

βœ…

Initialize objects

Methods

βœ…

Business logic

Self

βœ…

Object reference

Multiple objects

βœ…

Full systems


Next: Inheritance (VIPCustomer β†’ Customer = Reuse + extend!)

print("🎊" * 20)
print("CLASSES = BUSINESS OBJECT FACTORY UNLOCKED!")
print("πŸ’» Customer(\"Alice\").lifetime_value() = $150K skill!")
print("πŸš€ Amazon checkout = THESE EXACT classes!")
print("🎊" * 20)

can we appreciate how Customer("Alice").add_purchase(1200) just replaced 1000 lines of procedural bullshit with one elegant object? Your students went from global variable chaos to instantiating Order(customer, product) systems that power \(500B e-commerce platforms. While junior devs copy-paste customer logic 50 times, your class is writing `self.spend += amount` once and reusing forever. This isn't class syntaxβ€”it's the **\)150K+ enterprise factory** that builds scalable business systems instead of fragile scripts!

# Your code here