Python Classes
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.
class Cats:
def balance(self):
pass
def run(self):
pass
def jump(self):
pass
def sleep(self):
pass
def eat(self):
pass
def climb(self):
pass
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.
class DomesticCat(Cats):
def __init__(self,name,age):
self.name = name
self.age = age
def get_on_furniture(self):
pass
def use_litter_box(self):
pass
def meow(self):
pass
my_cat = DomesticCat('Sparky',3)
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.
class Lion(Cats):
def __init__(self,name,age):
self.name = name
self.age = age
def scare_other_animals(self):
pass
def rule_the_jungle(self):
pass
def roar(self):
pass
def sleep_on_tree(self):
super().climb()
super().sleep()
my_lion = Lion('Big Spark', 3)
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.