4-1 What is Object-Oriented Programming?

So far we have learned a few different ways of organizing data and code in our Python programs.   For example, we have used lists, tuples, and dictionaries to organize data, and functions and modules to help us organize code.   As we’ll see, objects are another way of organizing related data and functions together.

Object-Oriented Programming (OOP) is a natural way of thinking about designing solutions to programming problems that mirrors how we solve problems in the real world.  OOP has a lot of fancy terminology to describe simple ideas — get your glossary ready!

OOP Terminology

Consider the task of building a house.  To build a house you don’t just go outside, dig a hole, and start hammering boards together! First, you create an abstract design for the house you plan to build, called a blueprint.  The blueprint only describes a house, it’s not actually a house — you can’t live in a blueprint!

Once you have fine-tuned the blueprint you can go ahead and build as many houses as you need based on that design. Each physical house that you build is an instance of your house blueprint.   Each house instance will be structurally the same (e.g., # of floors and bathrooms), but may also have some minor cosmetic differences (e.g., paint colour).

Similarly, before an object can be created and used in our Python programs, it needs to be designed by a programmer. A class is Python code that describes data (called instance variables) and functions (called methods) related to a particular kind of object. Once we have written the code for our class we can then instantiate (which means “to create”) one or more objects of that class that we can then manipulate in our program.   We’ll get into the Python details of this next time.

A Real-World Example

 

To help you better understand these concepts, let’s consider a digital alarm clock.   An alarm clock typically needs to keep track of the current time, the alarm time, and whether the alarm is currently set or not.   As a user of the clock, we need an easy way to change any of these data values without understanding how the internal electronics of the clock actually work.  To change the clock settings we rely on various buttons on the clock that we can press to indirectly manipulate the clock settings.

In OOP terminology, an alarm clock object is an instance of a clock design blueprint or class.   The time and alarm state data stored within the clock object are its instance variables.   The buttons on the clock are the methods(or actions) that we can perform on the data stored within the clock, such as toggling the state of the alarm or changing the time.  The methods (buttons) of the clock object are designed in such a way that the user of the clock can never set the current time, alarm time, or alarm mode to an invalid state.  For example, it would be impossible to set the time to 25:89pm or set the alarm to “maybe”.

The Road Ahead

Languages like Java, C++, and Python are all object-oriented.  However, in Java and C++ every program must be written using OOP techniques.  This makes short programs unnecessarily complex and requires a lot of explanation before a new programmer can do anything significant.  Python takes a hybrid approach, you have all of OOP’s power at your disposal, but you can use it if/when you need to.  Python gives you power and flexibility.

The programs we have written so far have been relatively simple so we did not need to write them in an object-oriented way.   As we get into game development, Object-Oriented Programming techniques will be essential!

You Try!

    1. Start a new page in your Learning Journal titled “4-1 What is Object-Oriented Programming?”.  Carefully read the notes above and in your own words summarize the key ideas from each section.  Think of it as writing a “cheat sheet” for this lesson.   Making these notes will help solidify your understanding of the material and serve as a future reference to help you review key concepts. 
    2. It is said that an object “knows stuff” and can “do stuff”.  Explain what this means in terms of object-oriented programming.