Python class – Learn classes, objects and inheritance in Python

The class in Python

Grouping similar type of tasks into classes is termed as Object-Oriented Programming.  Where Class is a combination of variables/properties and functions/methods. This chapter does not aim at explaining the object oriented programming concepts thoroughly. The aim of this chapter is how to create and use Python classes.

Python Class

A Class in Python is a collection of class variables and functions. Where functions work with these variables. A python class, in general, can be taken as a template that is the basis of many instances in the form of objects.

For example, let us take a real world example, a model of a to be launched new car can be taken as a class. Once a model is approved, the cars made on the basis of that approved template or ‘class’ can be taken as objects. In this tutorial, we will use the example of a car class to explain how the classes can be created and used and different features of classes available in Python.

Syntax of defining a class

class NameofClass:

    ‘Class purpose/documentation string’

    Statements to be executed, data attributes and functions

  • As you can see, a class in Python is defined by the keyword class followed by the name of a class and a colon (:).
  • Next line will be with indentation, where it has documentation string that may describe class function/purpose.
  • After that, statements, class variables, and functions are created.

Defining a simple class example

The example below shows how to define a basic python class with a function.

Python Object

As in above example, a class is defined. Now it is time to create an instance of that class, an object, and run its function. Copy these two lines in the above code:

Running the program will give this output:

this is test class

Initializing class with __init__ method

As a class instance is created, it creates an empty object. However, in common practice, class objects are created with specific initial state.

In order to initialize a class, Python provides the __init__ method. Let us extend the above example and show you how it works:

The output will be:

This is initial state method

As you can see, the print function in the __init__ method displayed the string as an object of the class is created. That means as a class is instantiated, it automatically invoked the __init__ method and executed its statements.

A more practical Python class example

As an example was given in the intro section; taking car model as a class and manufacturing cars on that basis are objects. Let us use that example into a Python class code and then creating objects on that basis.

The output will be:

Car price is: 20000

New model color: White

Python Inheritance

When a class is created by ‘inheriting’ function(s) of another class then this process is called Inheritance. The class being inherited is called the parent class while calling class is called the Child class. The child class will inherit variables and one or more functions from the parent class.

Syntax of Python inheritance

Inheriting a class in Python is pretty simple. You just have to include the parent class in parenthesis after child class name.

class Child_class (NameofparentClass):

    ‘Class purpose/documentation string’

    Statements to be executed, data attributes and functions

Taking the car example again, let us say a car’s model (class) is approved and on that basis hundreds of thousands of cars are in the market for sale. Now the company has decided to give an added feature in a limited addition of that model. All of the features will remain the same in the limited addition plus one added feature. So you don’t have to create a complete new model. Just ‘extend’ the existing one and add the new feature in it.

Python inheritance in car model class example

Python Multiple Inheritance

Python supports a form of multiple inheritance. You just need to include multiple parent classes in parenthesis as below:

Was this article helpful?

Related Articles

Leave A Comment?