Classes and objects
How to study this week. These written lessons are the whole course; there are no videos. Read one lesson a day following the day-by-day plan, type and run every example, and do each Try it before moving on. Reading time: about 3 hours. Practice time: 7–9 hours.
What you'll build by Sunday: a small Python package for managing a library, with a proper folder structure, custom errors, JSON storage, and at least 8 passing tests.
Why this week matters for AI: LangChain, LangGraph, FastAPI and Pydantic are all built from classes, type hints and dataclass-style models. After this week you'll be able to read their source code and error messages instead of guessing.
The problem classes solve
In Week 1 an expense was a dict, and functions like add_expense(expenses) operated on a list of dicts. That works until the program grows. Then nothing stops someone from writing {"amont": 250} (a typo), and the data and the functions that change it live in different places.
Figure 2.1: A class is the blueprint (which fields and methods exist); each object is one filled-in copy with its own data.
A class bundles data (attributes) with behaviour (methods) into one new type. An object (or instance) is one concrete thing made from that class. BankAccount is the blueprint, and Rahul's account is an object.
When you call , Python actually runs . is simply the object the method was called on. Every method takes as its first parameter, and every attribute is read and written through .