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:
Create YOUR customer + product
Run business flow
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 150K+ enterprise factory** that builds scalable business systems instead of fragile scripts!
# Your code here