The Elegance of Classes

Understanding why we use classes and why it’s a holy blessing.

The Elegance of Classes

Section 4: Objects and Classes

If you are someone who used to code back in the late 1960s, then you know how hard writing pages and pages of code were. Today, we are effectively using object-oriented programming and our lives have become easier. Back in those days, procedure-oriented programming was used which focused mostly on functions, whereas object-oriented programming focuses on objects.

Let’s dive into understanding what objects actually are and how to create them.

Objects and Classes

To understand better, let’s take an example of a virtual zoo. Say, we want to create a virtual zoo, wherein in total there would be 2 dogs, and 1 dinosaur (you knew Dinosaurs were coming), and each with a unique nickname.

To create this database using procedure-oriented programming, it would not only require lists and loops to create them but as hundreds and thousands of various kinds of animals are brought in, the code would not only be lengthy but extremely hard to debug in case of any errors.

Enter Object-oriented programming.

In object-oriented programming, we create a blueprint defined as a class, so that we can create multiple objects using it. This is best explained in an example, so here’s how to define a class:

Class

Here, there are a few things to put your eye on. Let’s look at them all.

  • Class: To define a Class, we use the class keyword and initialize it with the name of the class. Note: The name is in Camel-case.

  • Constructor: init is a dunder method, a special kind of function. We will discuss dunder methods later but what it basically does is help us construct any attributes of the Class hence called a constructor.

  • Attributes: So what exactly are attributes? It’s a fancy term used for variables inside the Class.

  • Method: Method is another fancy term used for Functions inside a Class. It’s generally a good idea to know the terms as they are widely used but none of the basic idea changes just cause they are inside a Class.

  • self: When I got first introduced to object-oriented programming I had a tough time understanding what self does. I will be explaining in detail in the next blog post but for now, accept it as something that allows the object; we will create to access both the attribute and the method inside a class. It will become a bit more clear when we understand Objects. 👇🏼

Objects

Try Right-Clicking on the image, then select Open image in a new tab if the image is unreadable.

After declaring the Class (previous image) with the file named mammal.py we import the Animal Class. It’s not necessary but recommended as separating your code into two different files makes debugging easier. Once imported we create the object, let’s dive into the code and how it works internally.

  • Creating Objects: We create two objects of the name dog1 and dog2 using the Animal class, just by assigning two different variables to store the Object into. I have used keyword arguments to declare the values but can be done using positional arguments too.

What exactly happens under the hood is when I declare the Animal class and pass on the name & the breed it constructs the Object based on the blueprint of the Animal Class which is defined by the init method. Here the self word helps in connecting the arguments declared while creating the Object in main.py with the attributes inside the Class Animal in mammal.py.

  • Calling the Method: Calling the breathe() method on each of the variables inside the main.py would call the Function inside the Animal Class and do whatever is defined inside the Function. Here’s it to print 'Inhale - Exhale'.

What exactly happens under the hood is when the method is called on the Object created, it passes the Object (dog1, dog2, or dinosaur1) on the self parameter and then calls the Function to do the action defined inside the Function. This means that dog1.breathe() translates to Animal.breathe(dog1).

Note: While calling a Method, not providing the parenthesis would not run the code instead the location the Object is created will be printed

  • Changing the Attributes: You may have noticed that I have changed the value for tail for the Object dog2 as False. One of the blessings of using object-oriented programming or OOP is that you can change and create variables on the go.

dog2.tail = False is an example of changing the value. dinosaur1.horn = True is an example of adding a new attribute to the Object.

Conclusion

We have now learned the essence of Classes and why it makes our life easier without having to write redundant lines of code. Object-Oriented Programming is the way to go for us. While we have only scratched the surface of OOP, a lot more is there to unveil. And I will be talking about them in the upcoming blog posts, making them easier for you to grasp.

If you have any interesting suggestions or feedback, feel free to connect with me on Twitter. I also have a newsletter, which I send out every week Thursday. You can subscribe it here.