Skip to content
Crayon CS

Make it comfy

Lesson style

Quick mode shows the key picture, a summary and the practice. Good for a refresher.

Theme
Font

Both fonts are designed to be easy to read. Some readers with dyslexia prefer Lexend.

Text size
Spacing
Animations

Lesson 22 of 26 · about 17 min

Objects and classes

A class is a blueprint for a new kind of value. Each object made from it bundles its own data (attributes) with the things it can do (methods).

🧭 What you need first: Dictionaries and sets

The story

A cookie cutter isn’t a cookie. It’s a shape that describes what every cookie will look like. Press it into dough five times and you get five cookies: same shape, but each one is its own cookie. You can put sprinkles on one without touching the others.

A class is a cookie cutter for data. class Dog says: “every dog has a name and an age, and every dog can bark.” Then rex = Dog("Rex", 3) and fido = Dog("Fido", 7) are two separate objects, each with its own name and age.

You’ve been using objects all along: every string is an object of class str with methods like .upper(). Now you’ll design your own types.

Another way: a form template

A job application form (class) has blank fields: name, email, experience. Each filled-in copy (object) has its own answers. The form also comes with instructions (methods): “sign here,” “submit.”

Another way: a car model

“Toyota Corolla 2026” is a design (class). The cars in a parking lot are instances (objects). They share the design, but each has its own color, mileage and fuel level (attributes), and each can drive() on its own.

Play with it

Two objects, one blueprint

Step through. Watch __init__ run when each account is created, and notice that self is the account being worked on.

Watch the code run, one step at a time

  1. 1class Account:
  2. 2 def __init__(self, owner):
  3. 3 self.owner = owner
  4. 4 self.balance = 0
  5. 5
  6. 6 def deposit(self, amount):
  7. 7 self.balance += amount
  8. 8
  9. 9ada = Account("Ada")
  10. 10alan = Account("Alan")
  11. 11ada.deposit(50)
  12. 12alan.deposit(20)
  13. 13ada.deposit(5)
  14. 14print(ada.balance, alan.balance)

➜ yellow = the line about to run · ✓ = the line that just ran

Name it

The real words

  • A class defines a new type. By convention class names use CapitalWords.
  • An object (or instance) is one value made from a class: ada = Account("Ada"). Calling the class like a function creates a new object.
  • Attributes are the object’s data, stored on self: self.balance. Read or change them with a dot: ada.balance.
  • __init__ (say “dunder init”, for the double underscores) is the constructor: it runs automatically when an object is created, to set up its attributes.
  • Methods are functions defined inside the class. Their first parameter is always self: the object the method was called on. ada.deposit(50) is really Account.deposit(ada, 50).
  • __str__(self) returns the text print(obj) shows. Without it you’d see something like <Account object at 0x10f3a>.
  • Why classes? They keep data and the code that works on it together. That makes larger programs easier to understand: an Account knows how to deposit; the rest of the program doesn’t need to.
  • Good design: change attributes through methods (deposit, withdraw) that can enforce rules (“no negative deposits”), instead of reaching in and editing balance directly. This idea is called encapsulation.
Build it

A bank account with rules

Finish the BankAccount class. deposit(amount) adds money. withdraw(amount) takes money out, but if there isn't enough it raises ValueError and leaves the balance alone. __str__ returns e.g. 'Ada: $50'.

Challenge

Design a class from scratch

Write a Rectangle class: Rectangle(width, height), with methods area(), perimeter() and is_square().

Remember it

Inside a method, what does self refer to?

Got it?

Finish the lesson to color it in on your map and collect its concept cards.

Did this lesson click?