Chapter 9 Classes and Object-Oriented Programming

So far, this book has discussed using a procedural programming style to write computer programs. Procedural programming focuses on defining functions (procedures) as sequences of instructions to execute. While you’ve used control structures (e.g., conditionals and loops) to redirect the program flow, and data structures (e.g., lists and dictionaries) to organize data, you have designed programs primarily based on which lines of code execute in what order.

Object-oriented programming is a programming paradigm (style) in which you don’t design programs primarily around the sequence of statements executed, but around the specification of objects which are combinations of data (variables) and behaviors (functions). Objects are designed to represent elements in the program’s real-world problem domain, and provide a useful abstraction when designing programs: instead of needing to think about code statements executing in order, you can think about telling “things” to perform “actions”.

  • Using the turtle graphics module is an example of object-oriented programming—the “turtle” variable is a virtual version of a real-world robotic turtle, and you call methods on it to tell that virtual version to do things.

This chapter will discuss the fundamentals of object-oriented programming and how to specify classes (custom data types) that are used to define and create objects.

9.1 Why Objects?

The whole point of using object-oriented programming is to perform abstraction: we want to be able to encapsulate (“group”) together parts of our code so we can talk about it at a higher level. So rather than needing to think about the program purely in terms of integers, strings, and lists, we can think about it in terms of Dogs, Cats or Persons.

As an example, consider how you might represent information about a rectangle (e.g., in a drawing program). You could use individual variables to keep track of the rectangle’s size an position:

rect_x = 10
rect_y = 20
rect_width = 50
rect_height = 60

Of course, that’s a lot of variables to remember (and chances to make typos in the names). So you might instead think about encapsulating that data into a tuple or dictionary:

rect = (10, 20, 50, 60)  # as a tuple
rect = {x:10, y:20, width:50, height:60}  # as a dictionary (literal)
rect = dict(x=10, y=20, width=50, height=60)  # as a dictionary (using function)

This works well, until you want to have some way of associating functionality with that data—that is, you want to be able to tell the rect variable to do something (e.g., “calculate your area!” “draw yourself!”)

A Class (classification) acts as template/recipe/blueprint for individual objects. It defines what data (attributes) and behaviors (methods) they have. An object is an “instance of” (example of) a class: we instantiate an object from a class. This lets you easily create multiple objects, each of which can track and modify its own data.

In particular, classes encapsulate two things:

  1. The data (variables) that describe the thing. These are known as attributes, fields or instance variables (variables that below to a particular instance, or example, of the class). For example, we might talk about a Person object’s name (a string), age (a number), and Halloween haul (a list of candy).

  2. The behaviors (functions) that operate on, utilize, or change that data. These are known as methods (technically instance methods, since they operate on a particular instance of the class). For example, a Person may be able to sayHello(), trickOrTreat(), or eatCandy().

9.2 Defining Classes

9.2.1 Attributes

9.2.2 Methods

9.2.3 Constructors

9.2.4 String Representations