What is Constructor Overloading in Python? With Examples
By Rohit Sharma
Updated on Dec 30, 2024 | 5 min read | 7.2k views
Share:
For working professionals
For fresh graduates
More
By Rohit Sharma
Updated on Dec 30, 2024 | 5 min read | 7.2k views
Share:
Table of Contents
Python is a user-friendly programming language that can be easily mastered by beginners. This programming language offers a wide range of community support which helps the programmers in exploring several ways of implementing the code. The standard libraries in Python and the modules contributed by the community forum have allowed endless possibilities, including Web and internet development, desktop GUIs, database access, Education, scientific and numeric applications, network programming, software development and programs to develop games. This open-source language is developed under OSI approved license. Hence, it can be used and distributed free of cost. The Python Software Foundation administers the license of Python.
The main function of a Python constructor is to instantiate the objects. At the time of object creation, the class data members are assigned values or initialized with the help of constructors. The function _init_ is a constructor in Python. It is often called when a new object is created.
The Syntax for declaration of a Constructor is
Def _init_(self):
# constructor’s body
There are two types of constructors in Python. They are:
Default constructor:
It is a simple constructor and does not accept any input parameters. The definition of default constructors has only one argument that refers to the instance that is being constructed.
Parameterized constructor:
As indicated by the name, these constructors have parameters. The first argument of this type of constructors refers to the instance that is constructed and it is known as ‘self’. All other required arguments are specified by the programmer as inputs.
Check Out upGrad’s Data Science Courses
Construct overloading refers to a scenario of incorporating more number of constructors with a separate list of parameters such that each constructor is used to accomplish different tasks in the program. For example, there are four types of constructors in the Vector class.
Python does not explicitly support multiple constructors. However, the use of multiple constructors in Python can be accomplished in several ways. If a programmer writes multiple _init_ methods for the same class, the most recently written _init_ function overwrites all other constructors.
Python follows the “Readability counts” principle and emphasizes a clean and Pythonic coding style. While there is no direct support for constructor overloading in Python, there are elegant approaches to achieve the desired functionality. One way is to use default argument values, which enable the creation of constructors with different argument sets while maintaining clarity and readability.
When the user has a need of performing different actions to instantiate a class, the requirement of multiple constructors pops up. This is helpful when different actions are to be performed by the class on different parameters. The class constructors in Python are structured to exhibit polymorphism in three different ways, as listed below:
In this method, the constructors are overloaded by performing necessary action after the conditions for the passed arguments are checked. Let us consider an example of passing a parameter to the Sample class.
Calling Methods with _init_:
When an instance of a class is created, the class can have a single _init_ constructor that can perform any assigned task. This constructor can be modified to handle different functions and perform different tasks based on the parameter that is passed. Let us consider an example where the following calculations are performed.
Using the @classmethod Decorator:
In Python, we can define methods invoked on the class rather than an instance using the @classmethod decorator. Alternative constructors can be made Using this decorator that offers various methods of initialising objects.
We can use the @classmethod decorator or default argument values in Python to create several constructors. We may design constructors with various argument sets by implementing these strategies, providing flexibility in object construction.
In Python, generating objects or instances of a class is called instantiating classes. It entails calling the constructor for the object’s class, initialising its attributes, and preparing it for usage.
Python’s support for defining multiple class constructors is possible using the previous methods. We can offer different ways to instantiate and initialise class objects by including default argument values or the @classmethod decorator.
Python constructor overload techniques can be used to imitate numerous class constructors. This increases the accessibility and adaptability of our classes by enabling us to construct objects with various initialization settings.
Python provides several different strategies for delivering multiple class constructors. We have numerous ways to implement the necessary functionality, including method overloading, default parameter values, and the @classmethod decorator.
We can implement numerous constructors within our existing classes by examining the aforementioned methods. As a result, object customisation and instantiation are made more adaptable to accommodate changing requirements.
If you’d like to learn in-depth about method overriding in Python, we suggest you take up the 18-month Master of Science in Data Science. upGrad offers this course from IIIT Bangalore and Liverpool John Moore’s University. The course teaches you skills like Statistics, Predictive Analytics using Python, Machine Learning, Data Visualization, Big Data Analytics, and more. Students with a strong knowledge of concepts of Python are well-positioned to tackle subjects like Deep Learning, Natural Language Processing, Business Analytics, and Data Engineering.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
Start Your Career in Data Science Today
Top Resources