Introduction

Imagine you work for a company that locates missing persons. You have been assigned to a team that is writing an application to catalog missing persons and information about them in a database. Your code will define the information that's stored about each person, including a name and a photo, and provide logic for acting on that information. The application is written in Python, so you will also use Python.

You could write classic procedural code that contains functions, such as add_missing_person_to_database() and get_information_about_missing_person(). But this kind of code, although common, can be difficult to maintain. This kind of procedural code would be especially difficult to maintain over time as the code base grows in size and complexity.

Object-oriented programming (OOP) is a proven way to structure your code so the code is easier to write, understand, test, and maintain. Instead of leaving it to other programmers to figure out how to work with the photos you store and display them on the screen, for example, you could define an object that represents a missing person and build the capability to display that person's photo into the object. Anyone could then use a basic function call to render a photo of a missing person on the screen.

Writing object-oriented code in Python is a little different than writing procedural code, but if you know the basics of Python, it isn't difficult. In this module, you learn how to write object-oriented code in Python, and you also see first-hand some of its benefits.

Learning objectives

In this module, you will:

  • Write classes in Python
  • Add attributes to a class
  • Add methods to a class
  • Learn how to inherit classes
  • Add methods and attributes to an inherited class
  • Override methods inherited from a base class

Prerequisites

A basic knowledge of programming and Python.

Let's get started!