Home > Python > How to Implement Multiple Constructors For Class in Python?

How to Implement Multiple Constructors For Class in Python?

In this tutorial, we will learn what is python constructors and how to get multiple constructors for class in python?

Before proceeding with the multiple constructors class you must know about python constructors and how to initialize in the class.

So Let’s get started.

What is Constructors in Python?

A constructor is a special kind of method (function) that is used to initialize the instance of the class. Python considers the constructors differently but the C++ and Java declare the same name as the class.

There are two types of constructors:

  • Parameterized Constructor
  • Non-parameterized Constructor

When we create an object of this class, the constructor function will be run. Constructors also check if the object has sufficient resources to complete any startup tasks.

To create a constructor in python, we will use the __init__() function. When the class is created, this method is invoked. It takes the self keyword as a first parameter, allowing access to the class’s properties and methods.

Let’s see the example of python constructors in a class.

Class Constructor with No-Parameters

We can create a python constructor without any parameters to logging the data to keep the count number of instances of the class.

Let’s declare the constructor function with the __init__() function. See in the below example.

Output

Student Constructor
Student Constructor
Student Object Count = 3

Class Constructor with Parameters

Create a class constructor with parameters to initialize the instance of the variable of the class.

Output

Student Constructor
Student Name is Oliver and Phone is 1234567890

Hope you understand the python constructors and how to use them.

As you see in the above examples there are only one constructors and yes we can only create one constructor in a class…BUT if you could create multiple constructors in python class, isn’t it helpful for you to handle complex functionality in short?

Let’s get started with multiple constructors in the python class.

Create Multiple Constructors in Python Class

You can create multiple constructors in class and you can customize it according to the parameters. Constructors will be run based on the different number of parameters.

It helps when a specified class performs several functions based on various parameters.

Create Multiple Constructors with __init__() Function

As we learned above the __init__() function used to create constructors in the python class. It declares with the first parameter of a self keyword. The self keyword allows us to access the variables and functions of the class.

To create multiple constructors in the python class, we declare the __init__() function with argument. That argument will check which function has to execute.

It means the class will execute different functions based on that argument condition checking in the constructor function. See the example below.

Output

Equation 1 : 38
Equation 2 : -5
Equation 3 : 55

Create Multiple Constructors with Constructor Overloading

In this method, the constructor will overload again and again based on the arguments. Look at the example below.

Output

Square of integer: 64
Sum of list: 44
Hope you understand the Multiple Constructors.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment