OOP Part 1 — Classes, Objects & __init__
9. OOP Part 1 — Classes, Objects & __init__
9.1Blueprints & Objects (Classes)
Think of an architectural blueprint vs a physical house built from that blueprint. One blueprint can build 100 identical houses!
SHOW:
WHAT HAPPENED HERE
Student using class. Then we created two independent student objects from that single blueprint.THE TECHNICAL NAME
TAKEAWAY
9.2Initializing Object Attributes (__init__ & self)
Think of unboxing a new smartphone — during initial setup, you enter your name and phone owner settings.
SHOW:
WHAT HAPPENED HERE
__init__ ran automatically when Dog("Buddy", ...) was called. self points to the new dog object being set up.THE TECHNICAL NAME
__init__ method is the constructor. The self parameter refers to the current instance.TAKEAWAY
def __init__(self, ...): to set initial properties on new objects.9.3Adding Actions (Instance Methods)
Think of a bank account object — it has properties (balance) and actions you can perform on it (deposit, withdraw).
SHOW:
WHAT HAPPENED HERE
deposit is a method attached to BankAccount. It accessed and modified self.balance for that specific account.THE TECHNICAL NAME
TAKEAWAY
self.9.4Class vs Instance Attributes
Think of house rules (everyone living in the house shares the same address) vs individual bedrooms (each person has their own private room layout).
SHOW:
WHAT HAPPENED HERE
species is defined at class level and shared by all users. username is unique to each individual User instance.THE TECHNICAL NAME
TAKEAWAY
GOTCHA 1
Forgetting the self parameter in instance method definitions (raises TypeError when called).
GOTCHA 2
Confusing class attributes (shared by all objects) with instance attributes (defined in __init__).
GOTCHA 3
Using mutable class attributes (like list = []) which end up shared across every instance.
WHERE THIS SHOWS UP IN REAL CODE
Web frameworks like Django and FastAPI represent database records as Python classes (e.g., User, Order, Product).
TRY THIS YOURSELF
Define class Car with __init__(self, brand) and instantiate c = Car('Toyota').
Notice how buy() checks stock and updates self.stock in-place.
Demonstrate class creation, __init__ constructor execution, self parameter, and instance methods.
Stretch Challenge: Explain Classes & Build Shopping Basket.
Steps:
- Print explanation:
A class is a blueprint; objects are instances created from it.. - Class
Basketwithself.items = []. - Methods
add_item(self, item)andget_items(self). - Add
"Apple"and"Banana", printItems: ['Apple', 'Banana'].
What is a class in Object-Oriented Programming?
What is the role of the `__init__` method in Python?
What does the `self` parameter represent inside instance methods?
What is an attribute in Python OOP?
What is the difference between class attributes and instance attributes?
How do you instantiate an object `car1` from a class `Car`?
Why must `self` be the first parameter in instance method definitions?
What happens if you define `class User: items = []` at class level and append items?
Which keyword creates a new class in Python?
# comments · oop-basics
- @priya · 2h ago
The typewriter hero is such a nice touch.
- @alex · 1h ago
Totally agree! Made it super engaging.
- @sam · 1d ago
Finally a course where I can run code without leaving the page.