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.
Student Object Count = 3
Class Constructor with Parameters
Create a class constructor with parameters to initialize the instance of the variable of the class.
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
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.
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.
Square of integer: 64
Sum of list: 44
Hope you understand the Multiple Constructors.