Python Certification Training for Data Scienc ...
- 50k Enrolled Learners
- Live Class
Two methods cannot have the same name in Python. Method overloading in Python is a feature that allows the same operator to have different meanings. In this article, we will have a look at the method overloading feature in Python and how it is used for overloading the methods, in the following sequence:
Some of the advantages of using overload are:
Overloading a method fosters reusability. For example, instead of writing multiple methods that differ only slightly, we can write one method and overload it.
Overloading also improves code clarity and eliminates complexity.
Overloading can create confusion when used across inheritance boundaries. When used excessively, it becomes cumbersome to manage overloaded functions.
In Python, you can create a method that can be called in different ways. So, you can have a method that has zero, one or more number of parameters. Depending on the method definition, we can call it with zero, one or more arguments.
Now that you know what is method overloading in Python, let’s take an example. Here, we create a class with one method Hello(). The first parameter of this method is set to None. This will give us the option to call it with or without a parameter.
An object is also created based on the class and we will call its method using zero and one parameter.
#!/usr/bin/env python class Person: def Hello(self, name=None): if name is not None: print('Hello ' + name) else: print('Hello ') # Create instance obj = Person() # Call the method obj.Hello() # Call the method with a parameter obj.Hello('Edureka')
Hello Hello Edureka
To clarify method overloading, we can now call the method Hello() in two ways:
In the above example, we have created a method that can be called with fewer arguments than it is defined to allow. Also, it is not limited to two variables and your method can have more variables which are optional.
Now let’s take another example to understand method overloading in python.
In the following example, we will overload the area method. If there is no argument then it returns 0. And, If we have one argument then it returns the square of the value and assumes you are computing the area of a square. Also, if we have two arguments then it returns the product of the two values and assumes you are computing the area of a rectangle.
# class class Compute: # area method def area(self, x = None, y = None): if x != None and y != None: return x * y elif x != None: return x * x else: return 0 # object obj = Compute() # zero argument print("Area Value:", obj.area()) # one argument print("Area Value:", obj.area(4)) # two argument print("Area Value:", obj.area(3, 5))
The above code will give us the following output:
Area Value: 0 Area Value: 16 Area Value: 15
With this, we have come to the end of our article. I hope you understood what is method overloading in python and how it works.
To get in-depth knowledge on python along with its various applications, you can enroll for live Python online training by Edureka with 24/7 support and lifetime access.
Got a question for us? Please mention it in the comments section of this “Method Overloading in Python” blog and we will get back to you as soon as possible.