All courses
· intermediate

Object-Oriented Python

Bundle state and the behaviour that acts on it. Classes, instances, inheritance and composition, and the protocol methods that make your own types feel like Python's built-in ones.

Pathaxis · Original content

Written in house, from the work rather than from a syllabus. Every example is one we would use ourselves, and every exercise is checked against the same engine that grades your answer.

89 min 6 lessons 20 quiz questions 6 exercisesStart course

What you’ll learn

  • Define a class with __init__ and give each instance its own state
  • Write methods that act on the instance rather than on globals
  • Share behaviour with inheritance and call up with super()
  • Choose composition over inheritance when the relationship is has-a
  • Implement __repr__, __eq__ and __len__ so your types behave predictably
  • Cut boilerplate with dataclasses and guard attributes with property

Before you start

  • · Comfortable writing and calling functions
  • · Familiar with lists and dictionaries

Syllabus

01

Classes and instances

What an object is, and how state and behaviour end up in one place.

02

Relationships between classes

Sharing behaviour by inheriting it, and the cases where holding an object is better.

03

Python's object protocol

The methods Python calls for you, and the tools that write them.