learn
Weeks 10–12 · About 30 hours

Building your own kinds of thing

Classes let you bundle information together with the actions that work on it — and one object holding a reference to another is the idea every data structure ahead is built from.

Week 15 opens with “implement a from scratch”. A linked list is a class holding a reference to another object of the same class. Nothing more. If that sentence feels comfortable by the end of these three weeks, the next six months are navigable. If it does not, trees and graphs will be incomprehensible, and no amount of extra practice on them will help — the missing piece will be here.

Classes and objects

A class is a blueprint for a kind of thing. An object is one actual thing built from that blueprint, with its own information inside it.

Think of it likeThe class is the recipe for a cake; each cake you bake from it is an object. Same instructions, different cakes.

You write class BankAccount: and then, indented inside it, the functions this kind of thing can do. Those functions are called methods.

The special method __init__ runs automatically whenever you make a new object, and its job is to set up the starting information. The double underscores mean Python calls it for you rather than you calling it by name.

Every method takes self as its first parameter. self means “the particular object this was called on”. When you write self.balance = 0 inside __init__, you are storing a balance on this one account, separate from every other account. When you later write account.deposit(50), Python passes that account in as self automatically — which is why you never pass it yourself.

self feels arbitrary right up until the moment it does not. The thing that makes it click, for most people, is making two objects from the same class and watching them hold different values completely independently.

Where you meet it in real softwareUsers, orders, files, network connections — anything a program treats as a thing with properties and behaviour.

Why objects exist at all

They keep information together with the operations that are allowed on it, so the rules about that information live in one place.

Without classes you might keep a balance in one variable and write separate functions that change it. Nothing stops some other part of the program setting that balance to minus a million.

With a class, the balance lives inside the object and the only way to change it is through methods you wrote — a withdraw method that refuses to overdraw, for instance. The rule and the data sit together.

That is the whole justification. Not that objects are elegant, but that they give you one place to enforce what is true about a thing. As programs grow, that is what stops them becoming impossible to change safely.

Where you meet it in real softwareAny codebase of meaningful size uses this to keep its rules enforceable in one place.

Making objects printable

By default, printing an object shows something useless. Defining __str__ lets you decide what it shows instead.

print(my_account) with no __str__ gives you something like <__main__.BankAccount object at 0x7f9>. That is a memory location, and it tells you nothing.

Define __str__ to return a piece of text, and printing shows that instead. There is a sibling called __repr__ meant for programmers debugging, which is what shows up when an object appears inside a list.

This is a small thing that pays for itself immediately: when you get to linked lists and trees, being able to print a structure and actually read it is the difference between debugging in minutes and debugging for an afternoon.

Where you meet it in real softwareLogging, debugging, error messages.

Composition — objects holding other objects

An object can store a reference to another object. That single idea is what every in the rest of this roadmap is built from.

Think of it likeA train. Each carriage is coupled to the next one. No carriage knows about the whole train — it only knows what is directly behind it.

A Library object can hold a list of Book objects. Nothing new is needed for this: an object is just a value, so it can be stored like any other value.

Now make an object that holds a reference to another object of the same class. A Node with a value, and a next that points at another Node. Chain three of them together and you have built a linked list, weeks before anyone calls it that.

A tree node holds references to two children instead of one next. A graph holds references to several neighbours. Every structure ahead is a variation on this one move.

This is why the roadmap puts classes immediately before data structures rather than treating them as an optional extra. If composition is comfortable, the rest is variations on a theme. If it is not, the rest is memorisation — and memorised structures do not survive contact with a real problem.

Where you meet it in real softwareLinked lists, trees, graphs, and the object models of most real applications.

A gentle first look at recursion

A function that calls itself on a smaller piece of the same problem, with a rule saying when to stop.

Think of it likeBeing in a queue and wanting to know your position. You ask the person in front what number they are, and add one. They ask the person in front of them. The person at the very front knows they are first without asking — that is the stopping rule.

Every function needs two things. A : the simplest situation, answered directly without calling itself. And a recursive case: the function calling itself on something smaller, moving towards that base case.

Factorial is the standard first example. The factorial of 5 is 5 times the factorial of 4. The factorial of 1 is just 1 — no further calls. Each step is smaller than the last, and there is a floor to hit.

A missing or unreachable base case causes a RecursionError, which is Python telling you the calls piled up without ever finishing. Every call occupies real space while it waits, in a pile called the , and Python stops you at about a thousand deep as a safety measure.

You are not trying to master recursion here. You are trying to stop finding it frightening, because week 13 covers it properly and week 20 onwards assumes it completely.

Where you meet it in real softwareWalking through nested folders, reading nested data like JSON, and every tree and graph ahead.

Reading about this stage is not the same as finishing it. Type every one of these from a blank file rather than copying — the writing is the part that teaches.

BankAccount class

Methods to deposit, withdraw and check the balance. Withdrawing more than the balance must be refused rather than allowed to go negative. Make two accounts and confirm that depositing into one does not change the other — that is self doing its job.

Book and Library classes

A Book has a title and an author. A Library holds a list of Book objects, and can add one, list them all, and find one by title. Your first object that contains other objects.

Chain three Node objects by hand

Write a Node class holding a value and a next that starts as None. Make three of them. Set the first one's next to the second, and the second's next to the third. Then write a loop that starts at the first and follows next until it runs out, printing each value. You have just built and traversed a linked list. Sit with that for a moment — in week 15 this gets a name and a full set of operations, but the idea is exactly what you did here.

Recursive factorial and list-sum

Write factorial recursively. Then write a function that adds up a list recursively: the sum of a list is its first item plus the sum of the rest, and the sum of an empty list is 0. Write each one with a loop as well, and compare which version you find easier to read.

Exit checkYou can write a class from scratch, create two instances of it, and explain out loud what self refers to. You have manually chained node objects together and walked the chain with a loop.

If you cannot do this, repeat the stage rather than moving on. Nothing later gets easier by skipping it.

Worth knowing before you start

  • This is the second place beginners commonly quit. self feels arbitrary until it suddenly does not. Push through rather than skipping ahead — everything from week 15 onwards assumes it.
  • Do not move on until the Node chaining exercise makes sense. It is the single most load-bearing exercise in the first twelve weeks.

This stage corresponds to Phase 0 — Classes, Objects & Recursion Warm-Up in the source roadmap.