Constructor in Python
We have something called a constructor, which is used for instantiating the object. So, when the object is created, the task of the constructor is to initialize the attributes. In Python, there is a special method called __init__() method, which is the constructor, and it gets called when the object is created.
But now, you might be wondering that till now, we created objects, and we did not write any init method in our programs. So how is this happening, since we are saying that every time we create an object, the __init__ method is called? The thing is that if we are not writing the __init__ method, python provides a default constructor in such situations. It is called the default constructor, which is one of the types of constructors.
But the thing is that we want to initialize the objects, and for that, we need to write our own things in the constructors. Once you have a look at how we can write our constructors, and begin to use the constructors in your programs, you would become used to it.
So, first of all, Let’s have a look at a simple program, in which, we are going to write our __init__ method, and you would see that whenever the object is created, the __init__ method is going to be called.
Constructor in Python
As you can see in the above program, we have a class named GyaniPandit, and in that, we have the special __init__ method. Have a closer look at how we have created this method, we are going to need it a lot.
If we try to run the above program, the output comes out to be something like this –
The constructor called
The constructor called
The constructor called
As you can see, we have created three objects, and the constructor was called three times, which makes it clear, that the constructor is called every time the object is created.
Now, Let’s have a simple program, where we are going to use the constructor to initialize the object. Have a look at the below program –
As you can see, in the above program, we have a class Person, and in the Person class, we have the __init__ method. In that method, we can see that we are initializing the name attribute as a self.name since self is the current instance of the class. This makes sense since otherwise, we were writing the object reference variable name instead of self, but now, we are going to write self there.
So, when the __init__ method would be called, the name for the object would be initialized to “GyaniPandit”, and the age is initialized to 25. But for all the objects that we create, the name and the age are initialized to the same value, and for that, we can create a constructor, which would take some arguments (more on this soon).
If you try to have a look at the output of the above program, you will find that the value for the age attribute for both the object is 25, but the thing is that different persons can have different ages and different names, but when we are creating objects, all the objects are being initialized with same values. To get rid of that, we can simply pass some arguments to the constructor, to specify the different values.
This is known as the parameterized constructor, which we are going to look at now. Have a look at the below program –
As you can see, we have the Person class again, with the __init__ method, but this time, the thing is that we are going to pass some additional data, like the name and the age, because of this, the objects will be initialized with those values.
Also, you can simply see that we are giving the required data with the constructor in the program. So, when we are trying to print the ages of both the person objects, that would be 25 and 29 respectively.
So, all the stuff that we have discussed above, specifies the importance of constructors.
Now, we are going to address some different types of constructors. The thing is that we have seen those already, but this is the time to highlight their types.
Types of constructors in Python
Now, we are well familiar with the thing that the constructors are used for the initialization of the object. Whenever we create an object, the constructor is called. So, we are now going to have a look at some of the types of constructors. Let’s first list them, and then Let’s talk about them briefly. So, here are some of the types of constructors –
- Default constructor
- Non – Parameterized constructor.
- Parameterized constructor.
Now, we are going to consider the above-mentioned constructors, one by one.
Default constructor
At times, if we have not written a constructor, or we have forgotten to write one, python is going to add a constructor into the program, which is called the default constructor. Let’s have a look at a simple example, which tries to demonstrate the situation when we have a class, but we have not written the __init__ method.
As you can see in the above program, we have a class Person, and then we also have a walk method for the person. Then we are creating the person class object and call the walk method on that person.
We do not have the __init__ method written in the class here, but, since we have to initialize the object on our own, we need to define our __init__ method for the class. Here as well, there are two other types, which we are going to discuss now.
Non – Parameterized constructor
As the name of this type of constructor says, it is not going to have any parameters. Here, we are going to write our __init__ method in the class, and that method is not going to have any additional parameters. Usually, this type of constructor is used to initialize an object with some default value.
So, now Let’s have a look at a simple example, which demonstrates to us the non – parameterized constructor.
As you can see, we have written our own __init__ method in the class, but we are not giving any other parameter here. In the __init__ method, we are just initializing the attributes with their default values.
So, when we create any number of objects, they are going to have these attributes, with the default values.
You can try having a look at the output, to understand the concept better. This is right that we have seen this example earlier, but we need to highlight the type of the constructor as well, which is why this example seems to be repeated.
Parameterized constructor
The parameterized constructor is another type of constructor, and as the name suggests, we are going to provide the values for the initialization of the constructor. So, when we create the object, at that time, we also provide the values for the initialization. Let’s have a look at an example, which demonstrates the parameterized constructor.
As you can see, we again have the same class Person, and we have written our __init__ method here. Now, we have listed some parameters, which means that we are going to provide some data for the initialization of the object. When we are creating the objects, we are providing the name and age of the person, as required, and then we are printing the age of both the people, and we find that they come out to be different.
This way, we have seen the different types of constructors, and we can use them as and when required. Just remember some things about the constructors, like if we are not writing any constructor, then there would be the default constructor added by python. On the other hand, there are two more types of constructors, when it comes to writing our own constructors, which are non – parameterized, and parameterized constructors.