Python Class Functionality

In our previous lesson, we set up a new function to add more functionality to our Python class. We'll continue that lesson here.

Jump back to your MainFile tab. In PyCharm, notice that it has flagged up some errors:

Python argument errors

The error is "Parameter 'code' unfilled". The code was the variable we set up between the round brackets of init. PyCharm is telling us that we need to add another parameter, something to place inside of the code variable. Change your first object to this:

hero_one = Superheroes("luke cage", "IB")

Change your second object to this:

hero_two = Superheroes("jessica jones", "SS")

Your code in PyCharm should look something like this:

Python code to update an object

Now when we're setting up our objects, we're passing two things to it: the first is the name of the superhero and the second is a two-letter code. This will set values inside of the two instance variables self.full_name and self.superhero_code.

Let's now access the abilities class method that we set up.

Add this bold line to your code:

hero_one = Superheroes("luke cage", "IB")
super_one_name = hero_one.convert_superhero_name()
super_one_ability = hero_one.abilities()

Now add a line for hero_two:

hero_two = Superheroes("jessica jones", "SS")
super_two_name = hero_two.convert_superhero_name()
super_two_ability = hero_two.abilities()

Now amend your two print lines:

print(super_one_name + ': Superpower is - ' + super_one_ability)
print(super_two_name + ': Superpower is - ' + super_two_ability)

Your code should look like this:

Python code for two class objects

Now run your program. You should see this printed out:

Luke Cage: Superpower is - Impervious to Bullets
Jessica Jones: Superpower is - Super Strength

A Ratings Function

Let's now set up a rating system. We'll keep it simple. We'll pass over to our class a number between 1 and 5. The return value will be a simple string. If you pass over the number 5, for example, we'll return the string "Rating: 5 stars". Pass over a value of 1 and you can get back the string "Rating: 1 star". The point of this new method will be to show how to hide a class method from the outside coding world.

First, add another parameter to your init method. Call it rating.

def __init__(self, name, code, rating):

Store the value of this parameter inside a new instance variable called self.superhero_rating:

self.superhero_rating = rating

Here's your new init method:

Adding a new instance variable to our Python class

Let's set up a class method called get_star_rating. Add this to your Heroes code:

def get_star_rating(self):

star_rating = self.convert_score()

return star_rating

You should see that PyCharm has highlighted convert_score. This is because we haven't written this method yet. Add it to your code now:

def convert_score(self):

if self.superhero_rating is 5:

number_of_stars = 'Rating: Five stars'

elif self.superhero_rating is 4:

number_of_stars = 'Rating: Four stars'

elif self.superhero_rating is 3:

number_of_stars = 'Rating: Three stars'

elif self.superhero_rating is 2:

number_of_stars = 'Rating: Two stars'

elif self.superhero_rating is 1:

number_of_stars = 'Rating: One star'

else:

number_of_stars = 'Rating: No stars awarded'

return number_of_stars

It should look like this in PyCharm:

Adding a ratings function to our Python class

So the get_star_rating method calls the convert_score method. The text in number_of_stars is then returned and placed inside of star_rating.

The question is, which method do we call from our MainFile code? Do we call get_star_rating? Or we do call convert_score? The answer is that we call get_star_rating. We don't need to call convert_score. In fact, it would be preferable if we couldn't call convert_score from outside of the Heroes class. If you could call it from outside of Heroes class then it might cause problems and your program could crash. So how do we ensure that this method can't be called from outside of Heroes? Ideally, we'd like to make it private, which is a technical term used in programming. A private variable in most programming languages is one that can only be accessed in a very limited way.

However, there's a problem in Python. Well, not so much a problem as a feature of the Python programming language. It's this: Python doesn't really have private variables. If you want to hide a variable or method from the outside world, you can use a double underscore character.

Let's see how all this works.

In your MainFile code, set up a new variable calling rating. Add an equal sign and then hero_one:

rating = hero_one

Type a dot after hero_one and you'll see a popup list of available items you can add after the dot. One of these is our method, get_star_rating:

Class functions showing up on the PyCharm context menu

This is exactly what we want. But notice that our other method, convert_score , is also on the list. We don't want it to be on the list. If it's on the list of available methods then it means we can access it.

To hide it from the list above, go back to your Heroes code. Type two underscore characters before convert_score:

def __convert_score(self):

When you add the two underscores, you should notice that the convert_score in the get_star_rating method is highlighted:

Python object error

It's highlighted because PyCharm has spotted a problem. The problem is that no such method called convert_score exists. Because we changed it, it's now called __ convert_score. The two underscores are now part of the name. Add the two underscores and the highlight should disappear:

Adding underscores to a Python function

Go back to your MainFile code. Type a dot after hero_one and you'll find that convert_score is no longer on the list:

The function is now private and not on the PyCharm context menu

All this hiding methods and variables comes under the heading of encapsulation in programming lingo. In Python, you just have to get used to the fact that there's no true encapsulation.

Python Exercise

Amend your code so that the following prints to your output window:

Luke Cage: Superpower is - Impervious to Bullets, Rating: Four stars
Jessica Jones: Superpower is - Super Strength, Rating: Three stars

 

In the next lesson, you'll learn about another important class concept - Inheritance.

Python Class Inheritance >