Classes in Python.

Odeh Joyce
4 min readDec 18, 2020

A class is a blueprint that defines the variables and the methods common to all objects of a certain kind. It is a template of objects. It contains the code for all the object’s methods.

When do you use a class?

When you do have a set of data with a specific structure and you want to perform specific methods on it, you use a class.

Here is a simple example to demonstrate what a class looks like.

We have created a new class called ‘Phone’. To create a new class we use the keyword ‘Class’ and follow it up with the class-name. The class-name should always start with a capital letter.

Inside the class, we are creating two user-defined functions which is make_call() and send_message(). The make_call() method is printing out “making a phone call” and send_message() method is just printing out ‘Sending a message’.

Let’s see another example:

Line 1 — In creating a class, we use the “class” statement. Class names usually start with a capital letter (Dog).

Line 2 — Most classes do have a method called __init__. The underscores indicate that it is a special kind of method. It is called a constructor, and it is automatically called when someone creates a new object from your class. The constructor is usually used to set up the class’s variables.

The first argument to every method in your class is a special variable called self. Every time your class refers to one of its variables or methods, it must precede them by self. The purpose of self is to distinguish your class’s variables and methods from other variables and functions in the program.

Line 3 — print (“bark”). This function is to print the word “bark” when being called.

Line 4 — To create a new object from the class, you call the class name along with any values that you want to send to the constructor. You will usually want to assign it to a variable name. This is what the line Sam = Dog() does.

Let’s see another example of classes but this time, using inheritance.

Inheritance is a powerful feature in object oriented programming. It refers to defining a new class with little or no modification to an existing class. The new class is called derived (or child) class and the one from which it inherits is called the base (or parent) class.

The concept of inheritance is where you can create a class that builds off of another class. When you do this, the new class gets all of the variables and methods of the class it is inheriting from (called the base class). It can then define additional variables and methods that are not present in the base class, and it can also override some of the methods of the base class. That is, it can rewrite them to suit its own purposes. Here is an example:

Here, the Dog is the parent class and the Cat is the child class. We can see in the example that the cat inherited some of the properties of the Dog so it can use it without having to define it. The Cat also adds some features to the Dog class(parent class) under the __init__. One other thing we do notice from the Cat class(child-class) has overridden the Dog’s “talk method”.

You should know that when inheriting from a class, you indicate the parent class in parentheses in the ‘class’ statement. An importance of using inheritance in python is that It provides re-usability of a code. We don’t have to write the same code again and again.

Why do we make use of classes in python programming or why is it important in python programming?

- Modularity for easier troubleshooting

- Reuse of code through inheritance

- Flexibility through polymorphism and

- Effective solving problem.

Thanks for reading!

--

--