Python Quest

Learning map · Track 11 · intermediate

Object-Oriented Programming

Once a program tracks more than a handful of things — bank accounts, products on a shelf, shipments in transit, students in a course — keeping each thing's data in loose variables and dictionaries starts to hurt. Functions need long parameter lists, the same checks get repeated everywhere, and nothing stops a balance from going negative. Object-oriented programming lets you bundle data and the behaviour that belongs to it into your own types. This track teaches you to write classes, create objects from them, give them methods, control how they print, protect their data with properties, share behaviour through inheritance and composition, make them work with Python's operators and built-in functions through special methods, and cut boilerplate with dataclasses.

It is written for learners who already know functions, data structures, files, errors and modules. Every lesson is built around realistic models — wallets, invoices, inventories, appointment books, sensors, payroll records — and every exercise is a class you write and that is graded by creating objects, calling their methods and inspecting their state, including the cases that break careless designs: invalid amounts, empty collections, shared mutable defaults and forgotten super().__init__() calls. The track finishes with a step-by-step inventory and ordering mini project in which you decide which classes to write and what each one is responsible for.

By the end of this track you can

  • Define classes with __init__, instance attributes and methods, and create and use objects that keep their own state
  • Control how objects are displayed and compared with __str__, __repr__, __eq__, ordering methods and other special methods
  • Protect an object's data with validation, naming conventions and properties, and use class attributes, classmethods and staticmethods appropriately
  • Reuse and extend behaviour with inheritance, method overriding and super(), and choose composition when a has-a relationship fits better
  • Write concise record types with dataclasses, including defaults, default factories, frozen instances, ordering and __post_init__ validation
  • Split a small system into focused classes with clear responsibilities, using abstract base classes to define a shared interface

Lessons

  1. 1Classes and objectsWhy classes exist, how a class differs from the objects made from it, and how objects carry their own attributes.30 min · 4 exFree
  2. 2__init__ and attributesHow __init__ and self set up each new object, with default values, derived attributes and validation.35 min · 4 exMembers
  3. 3MethodsGive objects behaviour with instance methods that change their state or report on it, and chain calls by returning self.40 min · 5 exMembers
  4. 4String representationsControl how objects look when printed, inspected and put in f-strings with __str__ and __repr__.30 min · 4 exMembers
  5. 5Class attributes, classmethods and staticmethodsShare data across all objects with class attributes, avoid the shared-mutable trap, and add alternate constructors with classmethod.40 min · 4 exMembers
  6. 6Encapsulation and propertiesKeep an object's data valid with the _internal convention, @property getters, validating setters and computed attributes.40 min · 4 exMembers
  7. 7InheritanceBuild specialised classes on top of general ones, inherit their attributes and methods, and test types with isinstance and issubclass.40 min · 4 exMembers
  8. 8Overriding and super()Replace or extend inherited behaviour in a subclass, call the parent with super(), and let different classes answer the same method call.45 min · 4 exMembers
  9. 9CompositionBuild objects out of other objects, delegate work to the parts, and prefer has-a composition when inheritance doesn't fit.40 min · 4 exMembers
  10. 10Special methodsMake your objects work with ==, <, sorted(), len(), in, for loops and + by implementing Python's special methods.45 min · 5 exMembers
  11. 11DataclassesWrite record-style classes in a few lines with @dataclass, including defaults, default factories, frozen and ordered instances and __post_init__ validation.45 min · 5 exMembers
  12. 12Designing with classes: inventory mini projectDecide which classes a program needs and what each is responsible for, define shared interfaces with abstract base classes, and build an inventory and ordering system in milestones.90 min · 5 exMembers