Python Classes

In a previous section, you saw how to create a new Python module file and import it into your main project. The idea of having a separate file is to keep related code in one place. You can then use this code in other projects just by importing it. In this section, you'll learn about another way to group related code - in a class file.

A more structured way to group code, rather than having everything in functions, is with a class. A class can then be turned into something called an object. You can create multiple objects from your class, each object doing a slightly different thing. Let's see how classes work.

Create a new project. Click File > New in PyCharm and add a new Python file (or right-click your project name, PyClasses in the image below). Call this new file MainFile. Now add a second Python file. Call this one Heroes. Your project area in PyCharm should look like this:

PyCharm project area showing two Python files

Classes always start with the keyword class. After a space, you then type a name for your class. This should be a single name with no spaces. We'll call ours Superheroes. Add the following as the first line of your Heroes file:

class Superheroes:

Don't forget the colon at the end of the class name.

What we want to do with this class is have a superhero name, converted to title case. We want the superhero's special ability, and we want a ranking out of 5 stars. When we come to print out information about a superhero, we might see this:

Luke Cage. Superpower is: Impervious to Bullets, Rating is - Four stars

You pass information into your class. To get the above, we might pass in the following:

luke cage, IB, 8

Our Superheroes class will then take that information and process it. We can then get data back out in the format we want.

Let's deal with the superhero name first.

Classes usually have a function that sets up some variables. This is done in a function called init, short for initialisation. At its most basic, an init function looks like this:

def __init__(self):

self.variable_name = some_value_here

Notice the characters before and after init. They are two underscore characters: two before init and two after.

The keyword self is important. When you come to create object from your classes, self means this particular instance of the object.

After self, you can add your own parameters. We'd like to hand over to our class the name of a superhero. So we can add a parameter:

def __init__(self, name):

This is just like passing values to any function - it's just a variable name that contains a value you want to do something with.

What we want to do is to hand the name variable over to something called an instance variable:

self.full_name = name

The instance variable above is one we've called full_name. But you need the keyword self again just before it. Every time an object is created from our class, each object will have an instance variable called full_name. But your code should look like this:

Python code showing an init function in a class

The full_name variable will now be available elsewhere in our class code. What we want to do is to create a function that converts the name into proper case (also called title case). Let's add that now. Here it is:

def convert_superhero_name(self):

proper_case = self.full_name.title()
return proper_case

We've called the function convert_superhero_name. In between the round brackets of convert_superhero_name we again have that keyword self. When you create an object from your class, what's getting handed over to the self variable between the round brackets of the function is that object. It would look like this to Python, when the function is called:

ClassName.FunctionName(object_name)

Don't worry if you don't fully understand the keyword self. Just remember to use it as the first parameter for your class functions.

Your code should look like this:

A function added to a Python class

Notice what we have after the equal sign when we're converting to proper case:

proper_case = self.full_name.title()

We want to convert whatever was handed over to the full_name variable. We can do that with the inbuilt title method. Incidentally, the variable is called self.full_name and not just full_name. If you miss off the self you'll get an "Unresolved reference" error.

Let's put the class to work and create an object from it. We'll do that in the next lesson below.

Python Class Use >