• 12 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 1/26/21

Discover Object-Oriented Programming for Python

Log in or subscribe for free to enjoy all this course has to offer!

Understanding Classes

The relationship between classes and objects is like the relationship between a blueprint and a building. The blueprint defines where the walls should be, but the house is made of bricks and mortar.

With a class, you can instantiate objects based on that blueprint. Those objects have the same structure.

But what’s actually in a class?

A class contains state and behavior.

State refers to data or variables. A Person class might have a “name” variable, for instance.

A class’s behavior is simply a set of things that the class can do. This behavior is held in methods, which are identical in concept to functions.

For example, the Person class illustrated below is a blueprint specifying that a person has a name and can walk:

To the left, a blueprint of a person with the following information beside it: Name: [Name], and Walk(). Leading off to the right are two iterations of the blueprint, each with its [Name] field populated. One is named Alice, the other Bob. Both have the m

The blueprint has a “gap” for the person’s name. That’s because the blueprint itself is not a real thing that exists in the world. When you make (instantiate) a person from that blueprint, the real person has a name.

The blueprint Person also has the method Walk(), which means all of the people made from that blueprint do as well (i.e., they all come with the behavior of walking).

So what’s the difference between a method and a function?

A method is part of a class, while a function is independent of them. Methods can take parameters, modify the object's internal state, call other methods or functions, and return values.

Since a method is part of a class, it’s also part of an object that’s created from that class. The only difference between a function and a method is that methods are part of a class/object.

Understanding Objects

Unlike a class, which is just a template, an object is a real thing that can be passed around and used in your program.

Objects can be stored in a variable, and the type of that variable is the class. Just like the type of the number “5” is an integer, and the type of the word “hello” is a string, the type of a customer object is Customer.

You can modify the state of an object or call methods on it.

You can instantiate multiple objects from the same class, and those objects are independent. Knocking through a wall in your house - thankfully - doesn’t knock down the same wall in your neighbor’s.

I can already create variables and make functions, why make classes and objects?

Object-oriented programming (OOP) is a different way of thinking about programming than the procedural programming you will have done before.

OOP logically ties together data and the code that applies to that data.

Let’s look at an example of a Customer class. A Customer might have a name, a phone number, and a bank account - the state, and you might want to be able to bill or refund that customer - behavior.

Customer

State (variables)

name

phone_number

bank_account

Behavior (methods)

bill_customer(cost)

refund_customer(amount)

It makes sense to relate a customer’s information to the operations you’d want to apply to that information in this way.

A Word From Your Teacher

Is "Object-Oriented" the only type of programming? 

No, there are multiple programming paradigms. In fact, while Python was designed with a primarily object-oriented approach, the language also supports structural programming (procedural in particular), as well as functional programming.

Let's hear what Charlie Ann has to say about keeping an open mind about programming as you grow as a developer.

What Do You Wish You Had Done Differently When You First Started Learning Python?

Your Turn: Identify Classes

Now that you’ve got a basic grasp on the concepts, it’s time to try and apply it. You’ll need a piece of paper or a document editor open.

Below will be a few lines describing a situation. For each situation, identify a class and draw a simple diagram like the one above. Then identify any variables it should have based on the description, followed by any methods. Don’t add any attributes that aren’t mentioned in the description.

  • I have a toolbox. That toolbox can contain some tools, and I can add or remove tools from the toolbox.

  • My toolbox can contain hammers. Hammers come in various colors and can be used to hammer in and remove nails. I can also change the color of the hammer by painting it.

  • My toolbox can contain screwdrivers. They come in sizes (measured in millimeters) and can be used to tighten or loosen a screw.

Once you have your diagrams, hold onto them! You’ll use them in the next chapter when you start writing some Python code. To check if you’ve done them effectively, you can compare them with my examples here: .docx / .odt.

Let’s Recap!

  • Classes are templates for objects.

  • Classes combine state (data or variables) and behavior (methods) that apply to that data.

  • An object can be passed around and used in a program.

Now you know the basic concepts, it's time to write object-oriented code in Python.

Example of certificate of achievement
Example of certificate of achievement