Python Class Use


In the previous lesson, you set up a Python class. In this lesson, you'll import that class and put it to use.

Click on your MainFile tab. The first thing we need to do is to import our Heroes class. One way to do it is this:

import Heroes

However, you can have more than one class in your file. For example, we might want to add a class that covers heroes that are not of the super variety. We could add another class called OrdinaryHeroes. If we add import Heroes then we'd be importing both the Superheroes class and the OrdinaryHeroes class. We'd be doing that because they are both in the Heroes file.

A better way is to just import the class that you're going to be using. Add this as the first line of code to your MainFile:

from Heroes import Superheroes

Now we're just importing the class that we need - Superheroes.

Create an object from your class by adding this line to your code:

hero_one = Superheroes("luke cage")

You create an object on the right-hand side of an equal sign. On the left of the equals sign, you come up with a variable name to store your new object. This is known as an instance of the object. We've called our instance hero_one.

Notice that in between the round brackets of the Superheroes class, we have "luke cage". This gets handed over to that name variable we set up. The name variable is then stored inside of the self.full_name variable.

Function you set up inside of your class (they are called methods rather than functions when they are inside a class) can be accessed with this:


The method (function) that we set up was called convert_superhero_name. Our instance is hero_one. Add this line to your code then:


Your MainFile code should look like this in PyCharm:

Setting up an object from a Python class

Run your program. You should see this in the output window:

Luke Cage

So our class method, convert_superhero_name, has taken the lowercase luke cage and turned it into proper/title case.

We could also have done this, to make it more readable:

super_one_name = hero_one.convert_superhero_name()

Here it is in PyCharm:

Python code using functions in a class

But we're just returning the value from the convert_superhero_name method and placing it into a new variable called super_one_name. This is the same technique you'd use for any function/method. The only difference is that we now have the object instance name, hero_one, at the start.

We can set up another Superhero object quite easily:

hero_two = Superheroes("jessica jones")
super_two_name = hero_two.convert_superhero_name()

We're now setting up a new instance of Superheroes. This one is called hero_two. The code in PyCharm looks like this:

Code that sets up two objects from one Python class

Try it for yourself. Amend your own code to match ours.

When you run your program, this will print out:

Luke Cage
Jessica Jones

Adding Functionality to our Python Class

Let's add some more functionality to our class. What we'll do is allow a two-letter code to be passed over to our class. The class will then return a more explanatory string of text. For example, we could pass the letters SS over to a method in our class. The method will then return a value of Super Strength.

Add a new instance variable to the init method in your code:

self.superhero_code = code

Now add the code variable just after self, with a comma between the two:

def __init__(self, name, code):

In PyCharm, your code will look like this:

Setting up a new instance variable for a Python class

We can now add a function to check the value in self.superhero_code. Set up the following function, which we've called abilities:

def abilities(self):

superpower = 'No Superpower'

if self.superhero_code is 'SS':

superpower = "Super Strength"

elif self.superhero_code is 'IB':

superpower = "Impervious to Bullets"

elif self.superhero_code is 'HA':

superpower = "Hyper-awareness"

elif self.superhero_code is 'MG':

superpower = "Morphogenetic"

return superpower

We're just setting up a variable called superpower. By default, it's set to a value of "No Superpower". We then use an if … elif statement to check what's in the self.superhero_code instance variable. Depending on its value, we place a different string into superpower. Finally, we return the variable superpower. Your code should look like this:

An If statement added to a Python function

Let's now put the new class method to use. We'll do that in the next lesson below, as this page is getting a bit long.

Python Class Lesson Continues >