So you are building software that has to calculate the average of two integers. You wish to make a function or method for it to make it more readable. You just write a function that calculates an average of two integers.
However, you will find afterward in the software that you must also calculate the average of three numbers. Alternatively, you can require a function that can calculate the average of any parameters specified.
To calculate the average of any number specified by the user, you have two options:
The first one is to create functions like – avg_two (to average 2 numbers), avg_three (to average 3 numbers), avg_four (to average 4 numbers), avg_float (to average float values), and so on.
The other option is to create a single multi-function that accepts a variable number of arguments or datatypes and performs your operation based on the parameters you specify.
What is a convenient option? Undoubtedly the second one.
Why remember all of those distinct function names? In fact, why do you want to build so many separate functions with various names?
Now you must be thinking, is it even feasible? With a similar name, many functions with various inputs? Yes, It’s known as method overloading in python. Read this article to understand more about method overloading in python.
Method Overloading
Now we understand that there is no need to make different functions that do the task just because you have multiple arguments.
You would use this:
def avg(a1: int, b1: int):
…
def avg(a1: int, b1: int, c1: int):
def avg(a1: float, b1: float):
than this:
def avg_two(a1, b1):
def avg_three(a1, b1, c1):
def avg_float(a1, b1):
Method overloading allows you to call the same method several times. For example, we can call the same function average with multiple numbers of arguments.
How to define method overloading?
Method overloading is a characteristic of OOPS languages that allow us to have two or more methods with the same name but distinct arguments that they accept as input values. To be more explicit, the settings can be changed in three ways:
Now the question is, does python support method overloading? Unlike other languages, you cannot implement method overloading in Python by utilizing the same method name. Why?
Method overloading in Python by using a similar method name. Why?
Python interprets everything as an object, including classes and methods.
There are two ways of method overloading in Python.
1: Using the same methods differ depending on the data type of the parameters
To accomplish method overloading in python, we make a single function that uses numerous parameters to execute the same purpose.
For example:
class multiply:
def i_multiply (self, a1 = None, b1 = None, c1 = None):
p = 0
if a1 != None and b1 != None and c1 != None:
p = a1 * b1 * c1
else if a1 != None and b1 != None:
p = a1 * b1
return p
We used three variables as input in the code; however, their default values are None. This implies that if we only give two values when using the function, the third will be heeded as None by default, and the result will just be the multiplication of the two numbers given as input.
So, this is a function that can take two or three arguments as input and exhibits the same method overloading characteristics.
We have seen how to offer method overloading features to a function by setting arguments = None, but there is a better approach. The multiple dispatch module can be used to give your functions method overloading characteristics in Python.
We will build many functions with a similar name, and we will put a function decorator directly above the function.
@<decorator_name>
# using the decorator above the function def
def function(..):
Here’s how we will utilize the decorators for the functions to have the characteristics of method overloading:
from multi dispatch import dispatch
@dispatch(int, int)
def sum(a1, b1):
return a1 b1
@dispatch(int, int, int)
def sum(a1, b1, c1):
return a1 b1 c1
@dispatch(double, double, double) # initializing parameters as double
Here we will understand that the names of all the functions are the same, and we can use different parameter datatype method overloading using the decorators.
print(sum(72.1, 35.4, 59.6))
print(sum(5, 4))
print(sum(1, 4, 4))
Output:
152119.464
9
So, in this case, we just used the sum function with different parameters. The decorator has defined the data types for each function definition, which is dynamically dispatched based on the data types and the number of parameters we specified in the decorator.
The term dispatch means to transmit; thus, when the functions are called, the decorator transmits these definitions dynamically.
In Java, constructor overloading refers to the use of multiple constructors in an instance class. Each overloaded constructor, however, must have a unique signature. For the compilation to succeed, each constructor must have a unique set of parameters.
In object-oriented programming, python inheritance is a powerful feature. It refers to creating a new class with minimal or no changes to an existing class. The new class is known as the derived (or child) class, and the one it inherits from is known as the base (or parent) class.
Method overloading in python is a critical methodology, and the method is extremely effective in complicated circumstances requiring condition-based use and execution of certain parameters. The methodology must be implemented properly to eliminate redundant parameter use.
UNext Jigsaw is offering courses in Data Science and Machine Learning with a guaranteed placement feature. You should definitely consider pursuing a Data Science online course!
Fill in the details to know more
What Are SOC and NOC In Cyber Security? What’s the Difference?
February 27, 2023
Fundamentals of Confidence Interval in Statistics!
February 26, 2023
A Brief Introduction to Cyber Security Analytics
Cyber Safe Behaviour In Banking Systems
February 17, 2023
Everything Best Of Analytics for 2023: 7 Must Read Articles!
December 26, 2022
Best of 2022: 5 Most Popular Cybersecurity Blogs Of The Year
December 22, 2022
What Is The vi Editor in The Unix Operating System ?
November 25, 2022
The Best Agile Tools for Project Managers
November 24, 2022
A Brief Overview of the Unix File System
Introduction to Unix Operating System : Everything You Need To Know
Know Everything About AWK Advanced Filter
November 17, 2022
Web Developer Salary in India for Freshers in 2022
November 10, 2022
What Is the Use of Wrapper Class in Java?
March 22, 2023
What Is Clean Coding in Java?
March 21, 2023
What Are the New Features of Java 17?
What Is File Handling in Java?
March 16, 2023
What Is Data and Time Function in Java?
March 11, 2023
Top 10 Emerging Technologies Blogs To Read In 2023
December 15, 2022
Add your details:
By proceeding, you agree to our privacy policy and also agree to receive information from UNext through WhatsApp & other means of communication.
Upgrade your inbox with our curated newletters once every month. We appreciate your support and will make sure to keep your subscription worthwhile