Best way of using Python Classes

  • Reading time:8 mins read

What is a Python class?

A Python class is like a ‘framework’ for object creation. Python is an object-oriented programming language and therefore it relies on objects heavily.

 

Creating a Python class

 A Python class is created using the keyword “class” which is then followed by a space and the name of the class. The name of the class should always start with a capital letter and represent what the object does or at least mean something related.

In this example, we are going to create a Python class by the name “Elephant”. Note that all classes should begin with a capital letter as mentioned before. An then close it off with parentheses and a full colon.

Now, what comes next is quite important. We want to be able to initialize our object and our Python class every time we run it and we want to be able to assign specific variables to it. Now to do that, we would want to write our own __init__ function, which we do use the keyword  “def” followed by “ __init__”  and a pair of parentheses where we are going to pass our parameters.

 

Learn Python classes online

 

What this means, is that every time we run this Python class is going to initialize this and it’s going to assign parameters within the parentheses to the class. Now we want to choose the parameters that we want to be able to assign to this class. The first one always has to be self because it is within itself and the object we just created.

Apart from self, the other parameters are going to be named, age and kind. We now need to assign these parameters within this initialization function. We can do that simply if “self.name” is equal to name, “self.age” is equal to age and “self.kind” is equal to kind.

As shown in the code below.

 

 

This is going to let us access the parameters when we want to assign them to our classes.

Creating an instance of the Python class

We are going to start by creating an instance of a class and to do that we need to assign it to a variable. We do this by saying myelephant = Elephant(). Now, as we created the self.name, self.age and self.kind, we need to supply these arguments for our Elephant() class. So for the name, we will assign “Katie”, age will be 25 and for kind, we are going to say Indian.

 

Python for loop

 

When we print out “myelephant” in the output window it is represented as an object, this is because we have created our Python class object. If we want to access some of these arguments specifically, we can simply just do dot name at the end. Now, if we print that, it’s going to give us the name of this elephant that we have created.

 

 

What we can do is actually write a function within our class that can print out all the information that we’re interested in instead of printing it out individually.

 Let’s define our function and let’s do a summary. And within the function brackets, we have to pass self, you have to do this for every function within your class because it is referencing itself. And now we can say,  print self.name, self.age and self.kind.

 

 

When we call this function from our class that we’ve created, it’s just going to print out the information that we’ve given it. So now what I can do is instead of doing each of the individual ones separately, we can call the summary function from within our class that we’ve created.

 

Now since we already got a print statement in it, we don’t need the extra one. Now if we run this we’re going to see that all the data is printed out.

 

 

Now we can create multiple instances of this class. So let’s say myelephant2 and again we’ll do equals and we’ll go for the Elephant class and now we will supply it with variables Fred, 35 and African. Now we could say myelephant.summary(), but we also know, because we’ve created our second instance of this class, we can save myelephant2.summary(). When we run this, it will save the information that we’ve given it and print it out to the screen.

 

 

So what we’ve done is we’ve basically created our class with this one function that runs this information. And we’ve created multiple instances of that same class.

 

In our Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.