Subscribe to the Islander Newsletter

Wise choice future islander! Nuggets of gold shall be sent your way!
Email address
Secure and Spam free...
How to Create Classes in Python the Easy Way

How to Create Classes in Python the Easy Way

Do you want to know how to create classes in Python the easy way?

If so, then read on through this step by step guide that’ll show you how!

How to Create Classes In Python

How to Create Classes in Python the Easy Way

The first thing we must do before creating a class is define exactly what a class is.

A class is a sub-module in a Python file that consists of a series of function and commands to run.

It has a unique parameter in the “self” term that allows you to assign any variable you want to the class and certain variables and functions will print out when you run this. You’ll see where this comes into play in this tutorial.

Just like you would with functions, you will need to assign parameters to the class to print out certain variables.

So with that background said, let’s get into creating classes.

Assuming that you have your text editor opened up, the first thing you will do is open up 3 Python files like these:

How to Create Classes in Python the Easy Way

You can name them however you’d like; just follow the convention that I present in this post.

I want you to direct your attention to the 2nd file in the middle. Here, we will create the class that will be run in the first file.

So in this second file, I want you to write these lines of code on a sheet of paper and phrase in your own words what they do before seeing my explanation (you’ll better this way since you are engaging your mind).

How to Create Classes in Python the Easy Way

Let me explain what’s happening here and see if your guesses match up.

We first start with the class name so we know what information we can import into our main file, which will run our code.

Now notice that there is this __init__ function in the class.

This is known as an initiate function and what it does is it activate this self-term that will be unique to the class when we write it up.

It also activates the other parameters inside of the __init__ function to be activated when we assign the class to a self variable.

This self variable can be named anything you want when we define it in the main file along with the other parameters. As a result, they are independent variables.

However, the rest of the variables will be dictated by these inputs as you can see with the self. variables that are there.

These self. variables are unique to the initiate function because they involve this self parameter that you need to define for classes.

You can technically name these variables however you want while incorporating the self variable, but usually, they are written in this format:

self.parameter = parameter (with no quotation marks to indicate the parameter)

The reason is because it’s easier to remember what each variable prints out in this method.

And please note, you will only define the self.parameter variables in the __init__ function. The reason is because it only works under the __init__ function.

Now for the rest of the class, you can write any function that you want with any output you desire. This is known as Python Class Function where you write a function within a class.

Typically, the outputs are strings for Python Class Functions because you want to print out a quote for certain parameters, but the output can be anything in these functions.

Note in the Song function above that we wrote the self parameter in the parentheses. This designates the function to the class it’s in and when we type the self parameter with the function’s name, the self term will refer to the function in the class it’s assigned to.

You will see how this works in a moment.

Now I want you to write this line of code down and type this code in the Python file that’s set to run the code (don’t worry about the second line for now):

How to Create Classes in Python the Easy Way

The first thing we need to do is import the Africa class to our Python file. It is stored in the africa2.py file. If we don’t do this, the code will not run.

We will return to the second line later for the next concept I’ll walk you through.

The 3rd line designates our chosen self variable to the Africa class. In this case, the self variable is Evan.

Whenever we write Evan in this program, it will refer to certain features in the Africa class along with the corresponding variables that we wrote in as well.

For the next line, this will print out the content written under the Song function of the Africa class. The reason for this is because we assigns Evan to refer to the Africa class, it will automatically go into the Africa class and obtain the information under the Song function for printing.

As a result, the string “Africa by Toto” will print out.

Now, for the self.parameter variables, our self variable Evan will be placed in as the self in the self.parameter variable.

As you can see, each parameter has their own variable and the variables are assigned to the different parameters we entered, which will act as our data values. In this case, the data values are strings since our parameters are strings.

After all of this, the output is as follows:

How to Create Classes in Python the Easy Way

Now for the next part of the tutorial, we will examine a concept called inheritance where we import the class we just created to another class.

This is useful when we need to create different classes for things that perform some different functions, but maintain some characteristics.

Additionally, inheritance is more convenient than rewriting the entire code for the things the new class will perform, which will save time while writing the code.

However, for a function that we want a different output for within this new class, we need to redefine it, which you will see momentarily.

For now, go into the 3rd file we created and write on a sheet of paper the following code and type in:

How to Create Classes in Python the Easy Way

The first line gets the Africa class from the africa2.py file. If we don’t do this, the new Band class won’t run.

After we’ve imported the Africa class, we will now create the Band class that will incorporate the Africa class.

So we write the Band class, but to incorporate the Africa class in it, we simply write Africa in parentheses after writing Band.

All of the content from Africa will be incorporated into the Band class. When we run the self variable for Band, we will print out the content for Africa.

However, there is an exception and this exception is when we run the Song function.

For Band, we are giving the Song function a different output, so when we run Song with our Band class self variable, the output assigned to the Band class will print out.

So let’s see what this look like. Go to the Python file where the code will be executed and write this down on a sheet of paper and type the following in:

How to Create Classes in Python the Easy Way

Now you see, we have to write the 2nd line now to retrieve the Band class from the africa3 Python file we wrote it in.

Our self variable in this case is SpongeBob and this is unique to Band with the associated parameters that we have assigned to the Band class.

Every time we assign something in the Band class to the SpongeBob self variable, the outputs in the Band class (even if they are in the Africa class) will print out.

Now the next line after the Band class is defined to the SpongeBob self variable is SpongeBob.Song(). This will print out the print statement that we assigned to the Song function in the Band class.

The output for Song when we assign SpongeBob to it will be different than when we assigned Evan to the Song function since SpongeBob is the exclusive self variable for Band. Evan is the exclusive self variable for Africa.

In the next 3 lines, you will notice that we have print(SpongeBob.band, print(SpongeBob.lead_singer) and print(SpongeBob.song).

What will happen is because we incorporated Africa into the Band class, the same __init__ function will run with the parameters we assigned to Africa.

What I mean by that is the same variables we put into Africa will print for the Band class given the self variable for band (SpongeBob) is assigned to it.

So with this in mind, let’s run the code and see what we get for our outputs:

How to Create Classes in Python the Easy Way

As you can see, the string associated with the Song function in the Band class along with the parameters assigned to the Band class we printed out on the last 4 lines of this code.

And for your personal entertainment, I have this video for you to see:

I bet that’s you when you want the impossibly bad professors and impossibly hard material to go away!

I hope this post served as a positive step in the right direction for you to become an unstoppable super ninja in your studies and in your quest to get a job!

If you do have any question (literally, even if it’s the dumbest question in the world), I won’t just be more than happy to answer the question, but I will be the happiest person in the world to answer your question.

If you are an employer or prospective client, I hope this was useful in allowing you to see how I code and how I think.

I really want to see you succeed and it is very gratifying to my heart when you succeed.

Really, ask away or just comment with your thoughts on the post. I appreciate your feedback.

Until next time,

This is Evan signing off.

PS: If you got a lot out of this post and you want not just more Python tutorials, but extremely insightful posts that’ll shoot your productivity to the moon, unleash your full potential to achieve any goal you wish or if you are a college student, secret study hacks you can use starting today to send your GPA to the top, subscribe to Join the Island, the world’s greatest blog, below with your email!

PPS: I’ve got more Python tutorials here and here that show you how to make a multiple choice quiz and how to create a calculator in Python. Check them out now!

 

Evan Cruz
+ posts

Evan Cruz is the founder of Join the Island, the website committed to helping young adults become massively productive and reach their full potential.

He has been featured on Vox, OnlineU, and UpJourney. He has also a cited human relations expert and college expert.

He graduated Magna Cum Laude with a Bachelor of Science Degree in Civil Engineering.

Read more about Evan and Join the Island here.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Islander Newsletter

Wise choice future islander! Nuggets of gold shall be sent your way!
Email address
Secure and Spam free...