Classes and Objects
LockedLesson 16 of 60 · OOP · Medium
Finish these first:Functions →Or pass the tests below to unlock it now.
The big idea
A class bundles data (attributes) and behaviour (methods). self is the instance.
Think of it like this: Think of a Class as a cookie cutter (the blueprint) and each Object as an individual cookie stamped from it — each cookie can have its own frosting (state) while sharing the same shape (methods).
Read the lesson
A class bundles data (attributes) and behaviour (methods). self is the instance.
class Account:
def __init__(self, owner, balance=0):
self.owner = owner
self.balance = balance
def deposit(self, amt):
self.balance += amt
Class attributes are shared; instance attributes belong to each object.
Quick check
Before writing code: What should `(lambda s: (s.push(1), s.push(2), s.pop(), s.size())[-2:])(Stack())` return when your implementation of Classes and Objects is working?
Your challenge
Implement class Stack with push(x), pop(), and size().
More learning tools(compare languages, visual guide, related lessons)
Compare with another language
Similar to java.classes-objects (80% overlap). Explicit `self` in Python vs implicit `this` in Java; Java fields are declared.
Visual walkthrough
A diagram, a step-by-step animation, and short captions for Classes and Objects.
Learn this first
- FunctionsNot yet