Phase 1 · Programming Fundamentals
Object-Oriented Python
Model real-world ideas with classes, objects, methods, and simple inheritance.
On this page
Classes and objects
A class is a blueprint. An object (instance) is one concrete value built from that blueprint.
Use class ClassName: with CapWords names. Create an object by calling the class like a function: Robot().
class Dog:
pass
buddy = Dog()
rex = Dog()
print(type(buddy))
print(buddy is rex) # False — two different objectspass is a placeholder that means “do nothing yet.” Real classes fill in methods next.
__init__ and attributes
__init__ runs when you create an object. It sets up starting state.
self is the current instance. Attributes like self.name live on that object.
class Learner:
def __init__(self, name, day):
self.name = name
self.day = day
self.completed = False
ada = Learner("Ada", 5)
print(ada.name)
print(ada.day)
ada.completed = True
print(ada.completed)You never pass self yourself—Python injects the instance when you call Learner("Ada", 5).
Instance methods
Methods are functions defined inside a class. Their first parameter is always self.
Call them on an object: ada.advance(). Inside the method, use self to read or update attributes.
class Learner:
def __init__(self, name, day):
self.name = name
self.day = day
def status(self):
return f"{self.name} is on day {self.day}"
def advance(self):
self.day += 1
ada = Learner("Ada", 5)
print(ada.status())
ada.advance()
print(ada.status())Keep methods focused: one clear action or query per method.
Simple inheritance
Inheritance lets a child class reuse and extend a parent class. Write class Child(Parent):.
Override a method in the child when behavior should differ. Call super() to reuse the parent’s __init__ or methods.
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
return "..."
class Cat(Animal):
def speak(self):
return "meow"
class RobotDog(Animal):
def __init__(self, name, model):
super().__init__(name)
self.model = model
def speak(self):
return "beep-woof"
print(Cat("Miso").speak())
bot = RobotDog("Rex", "X1")
print(bot.name, bot.model, bot.speak())Prefer composition (an object holds another object) when “has-a” fits better than “is-a.”
Practice
Mini challenge: BankAccount
Create a BankAccount class with owner and balance. Add deposit(amount), withdraw(amount) (no negative balance), and summary() that returns a status string. Create one account, deposit, withdraw, and print the summary.
class BankAccount:
def __init__(self, owner, balance=0):
# TODO: store owner and balance
pass
def deposit(self, amount):
# TODO: add to balance
pass
def withdraw(self, amount):
# TODO: subtract only if funds allow
pass
def summary(self):
# TODO: return a string like "Ada: $120"
pass
# create account, deposit, withdraw, print summary
Interactive
Try It in the browser
Powered by Pyodide
Build a Counter class with value starting at 0. Add inc() to add 1 and show() to return the current value. Call inc three times and print show().
Output
—
Errors
—
Check understanding
Lesson quiz
Score 4/5 or higher to mark this day complete.