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 |
|
|
|
Order |
|
|
|
Product |
|
|
|
Inventory |
|
|
|
π 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}")
π Class Anatomy Cheat Sheet#
Part |
Code |
Business Use |
|---|---|---|
|
|
Create object |
Instance attr |
|
Per-object data |
Class attr |
|
Shared data |
Method |
|
Business logic |
Self |
|
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 |
|
β |
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