constructor in python
Constructor in python is a special type of class member function which automatically called when an instance of class (object) is created. Basically constructors are used to initialize member variable of class. Where destructors are used to destroy objects and perform final clean up. If you want to learn about classes and objects ( OOPs Concepts) , you can visit:
What is Constructor and Destructor?
Constructor is a special type of class member function which automatically called when an instance of class (object) is created.Basically constructors are used to initialize member variable of class.
The basic meaning of Constructor is making or building something. Technically, Constructor allocates memory to the variables for specific instance which have been created at any point in time. So Constructors also verify that there are enough resources for the object to perform any start-up task.
In simple words we can say constructor is used to initialize the data members of a class. After creating an object, there is no need to call the constructor function, it will call automatically.
Basically destructor means to destroy or release something. A destructor is a special member function that is called when the object is no longer needed. The purpose of the destructor is to free the resources that the object may have acquired during its lifetime.
Constructor in Python
In python __init__ method is used to create constructor. It is sometimes also called special function (magic or dunder ). Dunder stands for double underscores. In python special functions two prefix and suffix ( ex. __init__ ) underscores are used. In python, the name of a constructor is always the same, __init__(). When you create a class without a constructor, Python automatically creates a default constructor for you that doesn’t do anything. Every class must have a constructor, even if it simply relies on the default constructor.
Syntax of class using constructor:
Types of constructors:
In python, there are two main types of constructors :
- Non-parameterized Constructor
- Parameterized Constructor
1. Non-Parameterized Constructor:
A constructor with no parameters is called non-parameterized constructor. Here is only single required parameter i.e self . As shown in example below we need not to call __init__ function it automatically calls and gives output.
In above code, we create an constructor function __init__ which just prints a string using class’s data members. In line 6, object of that class is created named myobj. Now there is no need to call __init__ it will automatically called as shown in output below:
2. Parameterized Constructor:
The parameterized constructors are used to set custom value for instance variables that can be used further in the application. In parameterized constructor , we need to pass the required parameters of constructor, during object creation.
- In above code, there is a constructor __init__ which initiate the values of firstname and lastname .
- Here we also used second function fullname() to print string.
- As shown in line 10, Parameters are passed during object creation.
Constructor returns nothing, it is only used to allocate memory and to initialize variables. It will return an error if we add return statement to it as shown below:
Destructor in Python
Just like a constructor is used to create and initialize an object, a destructor is used to destroy the object and perform the final clean up. Destructor releases all the resources that are used by objects. Python also have garbage collector , to clean up memory. Garbage Collector cleans memory not only reserved by classes and objects but also other files , database connections etc. If we does not use destructor , the Garbage Collector will take care of the deletion automatically, which you never know when for sure.
In python __del__ is used to define destructor. Like constructor, it will call immediately when object is created, so there is no need to call destructor function.
So it is all about constructors in python in our further articles we will learn about some special functions that follows dunder(double underscores). Thanks
- Constructor is a special type of function which is used to initialize the data member of a class.
- Constructor called automatically whenever object created.
- In python __init__ is used to define constructor.
- Destructors are used to de-allocate memory or to destroy objects.
- In python __del__ keyword is used to define destructor .