puppy in teacup with red dots

Python is an Objected Oriented programming language. This means that Python has a data type called objects. Today we will discuss what objects are; how are objects relate to classes; and when you should use objects and classes.

What are Objects?

Objects are containers that hold a collection of attributes and functions. As an example, you might create an application that tracks dogs. For each dog you are tracking, you might create an object. Then for each dog object, there is a collection of attributes like:

  • Color
  • Age
  • Breed

Further, each dog object has actions associated with it. For example, you might:

  • Take the dog for a walk
  • Cut the dogs hair
  • Give the dog a bath

Each of these actions would be a function/method contained within the object. For example, let’s assume you have imported a library that gives you access to an object called dog. You could create a new dog object named Max by running:

mydog = dog(“Max")

You could set Max’s breed to Chihuahua by running:

mydog.breed=“Chihuahua"

And you could shorten Max’s hair length by calling the hair_cut function:

mydog.cut_hair(2)

Finally, you could look at all of the attributes using the print command:

print mydog.name
print mydog.breed
print mydog.hairlength

This should all make more sense after you read the next section titled “What are Classes?”.

What are classes?

As we discussed in the previous section, an object is a container that holds various attributes and functions. A class is the code that you use to create an object. In this section, we will create a class which can create dog objects like the ones we referenced in the previous section.

To create a new class, we just have to use the keyword class. Let’s create a new class called dog with two attributes: breed and name:

class dog:
  name = ""
  breed = ""

We can now create a new instance of this class by calling:

mydog = dog

Next, we set the name and breed of our dog the same way we would set any other variable. We have to call the object name and reference the attribute name we want to set:

mydog.name = “Max"
mydog.breed = "Chihuahua"

Now if we want to see what our dog’s name is, we can run:

Print mydog.name

This worked ok. But we probably want to treat the name of the dog as a unique attribute for each of our dogs. We also want to make sure that all of our dogs have names. To do this, we need to add an init function that will be called every time we create a new dog object.

class dog:
  def __init__(self, name):
    self.name = name
breed = ""

mydog = dog("Max")

print mydog.name

As you can see, when we create our new dog object, we can now pass in a name. The self.name call we make in the function is telling the interpreter that this object is going to be named whatever we pass in when we declare our function. We passed in the name “Max”. So when we print mydog.name, the output will be max.

Next, let’s look at adding functions to our class. There are various things we will do with our dog. Perhaps we need to cut our dog’s hair on occasion. In the class below, I have added a new variable called hairlength in our init function. I have also added a function called cut_hair:

class dog:
  breed = ""
  #Add hairlength variable to init function
  def __init__(self, name):
    self.name = name
    self.hairlength=10

  #declare hair cutting function
  def cut_hair(self, howmuch):
    self.hairlength = self.hairlength - howmuch

#create a new Dog object named Max
mydog = dog("Max")

#Call the function to cut the dogs hair
mydog.cut_hair(2)

#Print how long the hair lenth is now
print mydog.hairlength

As you can see above, we start by creating a new dog object, then we call the cut_hair function. The default value in the init function is to have a hairlength of 10. So the output from the print command at the end should be 8 if everything went as planned.

When should you use Objects and Classes?

Objects and classes allow you to break-up your application into smaller bits. These smaller bits can be independently modified and tested. And if you have done things right, you can modify one class without worrying about breaking another class.

As your programs get bigger, and you work on larger teams, this becomes a bigger and bigger issue. Generally, the rule is that classes should only do one thing, and do that one thing really well. Think of each class as a mini-program within your main program.

For more on this topic, I recommend reading about the SOLID principles of Object Oriented Design.

Summary

Today we have discussed what Objects and Classes are in Python. We have discussed how to use objects, how to create classes, and how the two topics are inter-related.