Object Oriented Programming

Alec Mills
4 min readAug 16, 2022

--

Understanding python as a data scientist is an important skill. It might not be the main part of what it takes for us to succeed in this field. But it will be something that will allow us to do our jobs to the best of our ability. This will be an intro into Object Oriented Programming also known as OOP. Next we will step into the fundamentals of creating classes, then into creating instances of those classes. Lastly we will then go into Methods and attributes. Hopefully this will give you the basic understanding to get you excited to test it out for yourself and learn more about Object Oriented Programming.

With Object Oriented Programming the basic idea is taking something that you would have to type over and over again, and putting it into a single member function or classes. When you want to use these objects or classes they will all have a set of attributes and methods that come with them.

STEP 1: Creating Classes and Instances

First to create a class all you need is to define it. But before we create one let’s look into why we want to have classes. For example what if you wanted to create multiple dogs that all would have the ability to eat, drink, play, and rest. You would have to create that same dog however many times you needed. Or we could just create a dog class then make instances of that class every time we wanted to add a new dog. Before we get into creating all of those attributes and methods that we talked about. We will start with just defining a class.

Yup that is it, it really is that simple! Now we will make a few dogs of that class and name them Clifford, Scooby-Doo, Beethoven, and Toto.

And that is it! You now have your class with a few instances of that class. Even though we set the names of these dogs equal to the dog class to create the instances, that doesn’t actually give them a name. So I’m going to change them to dog_1 …. dog_4. But we will give them the attribute of each name so don’t worry!

Step 2: Attributes

Now each dog is assigned the name it had originally. We will also set the breed of each dog as well. This will be the same thing but in place of name we will put breed.

Next we will introduce methods, they are essentially functions but for the class specifically. So unless you are using them with the dog class you won’t be able to call these functions. We will start by having each dog introduce themselves. To create a method you will define it and then pass all the parameters to the function. For these simple functions we will just be passing self to our functions. Self is a built-in keyword in python that will reference the the instance of the class you are using.

Step 3: Methods

Now you will remember that we had the class definition with the keyword pass in the definition. This allowed us to leave it empty. Now that we are adding methods we can remove it. Notice we were able to reference self in the definition. This will allow it to understand what dog will be introducing himself

After calling the method from each dog you can see how self was referenced and how it was able to find what name for each dog to print. Another example of methods would be play and rest. I went ahead and defined each of them and will show you how they work!

In the play method you can see that I gave each dog the energy attribute and that to play it requires 5 energy to play, and that if you rest then it will rest the dog back to 10 energy. This allows the dog to play two times before needing to rest. So you can now see how methods can interact with the attributes that the dogs have. Below you can see this in action.

I hope this gave you a decent understanding with the basics of Object Oriented Programming. For my next blog you will be able to learn about Inheritance for classes and I will link it below!

--

--

No responses yet