So far, we’ve used functions to organise code. Object-oriented programming (OOP) is a second way: we bundle related data and behaviour together into classes, then create objects from those classes.

Python is an OOP language at heart — every value you’ve ever used (a list, a string, a number) is an object. This section teaches you how to build your own.

What’s in this section

  1. Classes and objects — defining a class, creating instances
  2. Constructors and instance attributes__init__ and self
  3. Methods — functions that belong to a class
  4. Properties — controlled access to attributes
  5. Inheritance — building new classes on top of existing ones
  6. Data classes — classes with no boilerplate

We’ll keep the OOP simple here. The next section covers strict typing and Protocol, which is where modern Python’s type system really shines.

Toggle theme (T)