Phase 5: Object-Oriented & Modular Thinking · ~35 minutes · Python · uv
Classes and Objects — Bundling Data and Behavior
self isn't a keyword or a convention you follow out of habit. Forget it in a method definition and Python tells you exactly what's missing.
Hiring signal: Explains self correctly as the instance itself, not memorized boilerplate, and can diagnose a missing-self TypeError on sight
What you will learn
- Define a class with __init__, instance attributes, and methods
- Explain self as the instance itself, automatically passed — not magic, not optional boilerplate
- Diagnose a real TypeError from a malformed method definition missing self
- Predict an attribute's value after a method call that mutates it
Introduction
Type: Learn Languages: Python Prerequisites: Phase 04 (Computational Thinking & Algorithms) Time: ~35 minutes
Objective
Learning objectives
- Define a class with
__init__, instance attributes, and methods - Explain
self as the instance itself, automatically passed — not magic, not optional boilerplate - Diagnose a real
TypeError from a malformed method definition missing self - Predict an attribute's value after a method call that mutates it
What you're building
Take this Phase 02-style dict-based record:
student = {"name": "Ada", "grades": [92, 88, 95]}
def average_grade(student):
return sum(student["grades"]) / len(student["grades"])
def add_grade(student, grade):
student["grades"].append(grade)
A script (student_class.py) that:
- Converts this into a
Student class with __init__(self, name, grades), an average_grade(self) method, and an add_grade(self, grade) method - Creates at least two
Student instances and demonstrates that mutating one (via add_grade) doesn't affect the other - Contains a comment showing the exact
TypeError that would occur if average_grade were defined without self, and explains in one sentence why
A class defines def deposit(self, amount): self.balance += amount. Given account = Account(balance=100), what does account.deposit(50) actually do, step by step?
account.deposit(50) is shorthand for Account.deposit(account, 50) — self is automatically bound to account, and amount receives 50. Inside the method, self.balance += amount is exactly Phase 01's assignment-as-instruction pattern, applied to an attribute instead of a plain variable: it computes account.balance + 50 first, then rebinds account.balance to that new value. No new object is created; the same account instance is mutated in place, exactly like bump_grade mutated the same Student instance above.
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers The Problem, Check Yourself, Key Terms & Next — plus a hands-on lab, quiz, and project artifact.
Create a free account to unlock Phase 0 and Phase 1 of every course — no credit card.
Browse all courses · View pricing · DeVenture Academy