Classes and Objects — The Mental Model
You've been using objects since lesson one — "hello".upper(), my_list.append(x) — strings and lists *are* objects. Now you learn to build your own, which is how every large codebase stays organized.
The mental model: blueprint vs buildings
Think of a blueprint for a house:
A class is the blueprint. An object (or "instance") is each real house built from it:
# The blueprint
class Student:
pass # "pass" means "empty for now" — a legal placeholder
# Three real students from one blueprint
s1 = Student()
s2 = Student()
s3 = Student()
print(type(s1)) # <class '__main__.Student'>
print(s1 == s2) # False — different objects, same blueprintOne class, many objects — each independent. Change s1's data and s2 doesn't notice. This is why apps are built this way: one Student blueprint, thousands of student objects.
Objects hold: data + behavior
Every object combines two things:
You've used both forever:
"hello".upper() # upper = a behavior (method) of strings
my_list.append(5) # append = a behavior of listsNow you'll design both:
class Student:
def introduce(self):
print("Hi, I'm a student!")
s1 = Student()
s1.introduce() # Hi, I'm a student!A method is a function defined inside a class — called with a dot: object.method().
Adding data to objects (the quick-and-dirty way)
Python lets you attach data to any object directly:
class Student:
pass
s1 = Student()
s1.name = "Aarav" # attach an attribute
s1.marks = 92
print(s1.name) # Aarav
print(s1.marks) # 92It works — and it's how you'll first feel the object idea. But it has a flaw: nothing forces every student to *have* a name and marks. The professional way — the __init__ constructor — fixes that in the next lesson.
Why OOP? (the honest answer for beginners)
At small scale, functions and dicts work fine — you've built real apps without classes. OOP earns its keep when:
Don't panic about using it perfectly. This module's goal: read classes comfortably and write simple ones. That alone unlocks every framework tutorial on the internet.
The vocabulary card
| Term | Meaning | Example |
|---|---|---|
| Class | The blueprint | class Student: |
| Object / instance | One real thing from the blueprint | s1 = Student() |
| Attribute | Data attached to an object | s1.name |
| Method | A function attached to a class | s1.introduce() |
| Instantiation | Creating an object from the class | Student() |
✅ Checkpoint
s1 and s2 from the same class equal? *(No — different objects)*pass do in a class body? *(Placeholder — "empty for now")*Next: __init__ and self — giving every object its data properly.