If you’re an experienced programmer coming from another language like C++ or Java, one thing you’ll definitely try to implement in Python is Function overloading. If you try it the same way you’ve been doing things in C++ or Java though, you’ll quickly run in to a few issues.
This is because Python doesn’t natively support Function overloading, but we can achieve the same thing using the multiple dispatch system.
What is Function Overloading?
Function overloading is the act of creating more than one function with the same name, but with different parameters. The function that actually gets called, is the one whose parameters matches the function call.
Function overloading is used very often in Classes, which are literally comprised of methods.
How Python deals with Function Overloading
So what happens if you try declaring multiple functions of the same name, with different parameters? Well, Python will overwrite the previous functions with the latest defined one. So if you declared and defined three functions of the same name, Python will overwrite the first 2 with the third.
In the below example, trying to call the first add()
function will throw an error, as Python overwrites it with the add()
function which has three parameters.
def add(num1, num2):
print(num1 + num2)
def add(num1, num2, num3):
print(num1 + num2 + num3)
# Throws an Error
add(3, 4)
This gives us the following error on the console. As you can see, it’s expecting for you to have three parameters in the add()
function.
TypeError: add() missing 1 required positional argument: 'num3'
This brings us back to the Multiple Dispatch system.
Multiple Dispatch
To use this, you will actually have to first download and install the multiple dispatch library. This can be easily done by using the pip install multipledispatch
command in the command prompt.
Now all you have to do is add a single line above each function you define. This line defines the datatypes of the parameters being used in the function. The format is as shown below.
from multipledispatch import dispatch
@dispatch(int, string)
def function(ID, name):
...
@dispatch
is the keyword, and within the brackets comes the datatypes of the function parameters.
The Dispatcher works by creating objects of the different functions, and selecting the right one to be executed at run time.
from multipledispatch import dispatch
@dispatch(int, int)
def add(num1, num2):
print(num1 + num2)
@dispatch(int, int, int)
def add(num1, num2, num3):
print(num1 + num2 + num3)
# Calls the first function
add(3, 4)
# Calls the second function
add(3, 4, 1)
The output of the above code:
7
8
For our enthusiastic readers, you may be interested in another similar concept where instead of functions, we overload the operators instead.
This marks the end of the Python Function Overloading using Multiple dispatch Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.