Python 3 objects and classes featured image

Programming with Python 3: How To Go About Constructing Classes and Defining Objects?

Introduction

We all know about Python as a programming language. To be more specific, Python is an object-oriented programming language. In object-oriented programming, or OOP, the goal is to make reusable code patterns. This is different from procedural programming where we give a sequence of instructions.

Object-oriented code is especially useful when you are working on complex programs. When you use a language like Python 3, you can create code patterns that you can reuse. The reusable code makes for a readable and easily understandable program despite its complexity. Such code is also much easier to maintain in the long run.

Classes and Objects in OOP

If you are trying to learn more about object-oriented programming, then knowing about classes and objects is a must. You need to be able to differentiate between the two.

Think of a class as a blueprint. It is a blueprint for a given object as designed by the programmer. Understandably, the blueprint will comprise all the attributes that characterize that particular object. An object, on the other hand, is best defined as an instance of a class. This means that when the class manifests in the program, it becomes the object. We use classes to make patterns. The patterns are then used to make objects.

Going forward, we will learn more about classes and objects, and how you can construct them.

  • Classes

Let’s start with classes. As explained before, classes are the blueprint for defining objects. If you want to define a class, you need to use the class keyword. You will use this just as you would use the def keyword.

It is easier to understand the process with an example. Consider the following. Let us define a class named Shark. We want to associate two functions with this class: swimming and being awesome. Here is how you will define this class in Python 3:

As you can see, these functions are indented under the class Shark. That is why we call them methods. Methods are special functions within a class. Another thing you may notice is the use of the word self. We use this argument to reference the objects that we will create through this class.

It is important to know that simply defining a class does not create objects. It only means that you have made a pattern for a Shark object. We will discuss how you can make an object from this blueprint below.

  • Objects

Now, we will discover how to make an object or an instance using the previous Shark class. Let’s say we want to make an object called sammy under Shark. Here is how you would do it:

We are essentially saying that the object sammy is equal to Shark(). As such, we made it an instance of the above class.

Next, we will be using the two methods that we mentioned in the previous section. We will use the methods with the new Shark object, sammy:

As you can see, we are using the dot operator (.) with the methods. The dot operator allows us to make a reference to the attribute or method of an object. You have to use it in conjunction with the parentheses to call it. Using the self parameter makes sure that sammy object is passed to the two methods.

Next, you need to add something to the parentheses. To add the object in the context of the program, consider the following example:

Now, if you were to run your program, you will see something like this:

shark class python

As a result of your programming, the object sammy is able to call and run the two methods that we defined in the main() function of the program.

The Constructor Method

One handy thing to know with Python is the constructor method. Also called the __init__ method, you can use this to initialize data. It is the first definition of any class. Here is what it looks like:

Let’s consider the previous example once again. If you were to add this constructor or __init__ method to the Shark class, the program will give you an output like this:

Python 3 class with constructor method

The constructor method did not modify anything in the sammy instance. This is because the program automatically initializes the constructor method. So if you want to want to initialize some data without changing anything else, you can use this method.

On the other hand, you can make your own method using a name variable. You can use this to assign names to certain objects. To do this, we will have to pass name as a parameter. We will also setself.name as equal to name. Here is who to do it:

This is how to modify the strings in the functions to reference the names:

To make it a parameter of the Shark class, we will set the name of the object sammy as equal to “Sammy”:

Now, when you run the program, you will see this output:

Python 3 class with custom name

The program prints out the name that you set. Similarly, if you want to add another parameter, you can also pass it to the __init__ method. Let’s say we want to add age:

Now you can add Sammy’s age when you make the object sammy:

More Than One Object

Let’s come back to the point of using one blueprint to make several objects. You can use the same class to make many similar objects. In this example, we will add another object to the Shark class called stevie:

As you can see, we also passed the name “Stevie”. In regards to methods, we used be_awesome() with sammy and swim() with stevie. Now let’s see the output when we run the program:

creating more than one object

The above output shows us two different objects in the form of sammy and stevie. Both belong to the class Shark. In this manner, you can use the same class or blueprint for multiple objects without having to make new ones every time.

Conclusion

Programming does not necessarily have to be a tedious process. Object-oriented programming is one of the solutions designed to make this process simpler and more straightforward. You can easily write complex programs with reusable patterns and easy-to-read code. This makes future maintenance easier as well.

In this guide, we covered some of the concepts involved in OOP, such as classes, how to define them, and how to instantiate objects. We also talked about the constructor method and how to make multiple objects from the same class.

To build on your knowledge about the Python programming language, refer to our blog for more resources:

Happy Computing!