Python Classes

Alejandro Colocho
Nov 23, 2020 · 3 min read
Image for post
Image for post
Photo by: Ryan Quintal

If you are a human, then you will be familiar with animals. Cats, lions, cheetahs, leopards, and cougars are all animals in this wonderful world! Can you guess what type of animal I like? If you guessed canines, then you are wrong. Moving on, these animals have certain things in common, but they are very different too. This reminds me of python classes! This is good news because that’s what we will be talking about this week. Let’s begin.

Classes

A python class is a way to collect functions and data in a single place. This makes programming so much easier because it allows us to look in a single place for a functionality, edit that functionality in a single place (and implement it everywhere), and debug that functionality in a single place. To explore this concept, we are going to talk about it in terms of cats.

Just like we mentioned, cats have similar characteristics among their whole family. No matter the cat, they are generally really good running, jumping, balancing, and climbing. They all do basic things too, like sleep and eat. We made a class name Cats to represent this. The class is a collection of functions that all cats should be able to do. This is fun, but things get more fun when we look at lions and domestic cats.

As you can see, we made a new class called DomesticCat. Wait a second, why did we pass an argument to it? I’m glad you asked, that’s call class inheritance. This means that all the functionalities from Cats will be available in our DomesticCat Class. Our new class adds three functionalities to the collection which are get_on_furniture, use_litter_box, and meow. However, we can see that we defined a weird new function called init. The init function allows us to pass our class data and store it as an instance variable. An instance variable will be data (variables) that are unique to that instance as they can be changed for another instance. For example, we created an instance of our DomesticCat, and we stored it in my_cat. Now, if you are curious you are probably wondering why we have been using the word self so much. The word self is used to reference the instance, so we can access the various data included in it.

Now we created the instructions to create a lion. We created a lion by calling our class. Nothing has really changed besides new functionalities added on top of our Cats functionalizes. However, there is one important thing, we added a functionality that allows for Lion to sleep on trees. This functionality uses the Cat’s functionality to climb and sleep. In order to use those functionalities in our Lion class we use the super() method. Other than that, it’s business as usual.

The Wrap Up

The concepts that we learned are some of the basics of classes. However, classes do not stop there. For example, python classes can inherit from multiple classes. They are power tools that helps us create functionalities for our projects. Once again, this touches on OOP. We can create a collection of data in a central place, edit functionalities in one place, and debug in one place. It helps us minimize error and reduce development time.

Data Driven Investor

empower you with data, knowledge, and expertise

Sign up for DDIntel

By Data Driven Investor

In each issue we share the best stories from the Data-Driven Investor's expert community. Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Alejandro Colocho

Written by

Data Scientist | Gamer | Nature Lover | Gym Enthusiast

Data Driven Investor

empower you with data, knowledge, and expertise

Alejandro Colocho

Written by

Data Scientist | Gamer | Nature Lover | Gym Enthusiast

Data Driven Investor

empower you with data, knowledge, and expertise

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store