Method overloading is sometimes referred to as “polymorphism” and means that a method can have two or more different meanings at different places in a program’s execution. It should not be confused with method overriding which usually comes with object oriented programming and inherited classes.
In method overloading you have two different definitions of a method with the same name but different signatures (e.g., they take different numbers of arguments or different types of arguments or both). In method overriding you typically have two different definitions of a method, one with the parent class and one with a child class but they usually have the same signatures.
Here is a simple example with a Restaurant class:
As you can see from the code, the Restaurant class has two attributes which are name and address. The method “fullname” returns a string containing both attributes. We can now define a Restaurant and print its fullname by the following code:
The output will be:
Now let us overload the method. We will create another method with the same name inside the same class.
Now if we run our code again:
We will get the output:
What has happened here? Note that the fullname() method now invokes the new definition and it is as if the old definition does not exist at all! Furthermore we haven’t explored the full notion of overloading because the two methods have the same signature.
Let us complicate matters by updating the signature of the second fullname() method.
Now the same code as before gives the output:
We get an error indicating that Python is expecting the middle argument in the invocation we are making to the fullname() method, even though there exists another fullname() method that doesn’t require that argument. In other words, Python is really not doing any method overloading! When you give multiple method definitions, Python simply takes the most recent definition, forgets any previous definitions, and expects you to follow the invocation pattern of the most recent definition.
If we update our driver code:
We get no errors and the output:
So can we implement method overloading at all in Python? It turns out there is a sly way to do it using a generic argument to your method that you then unpack inside the definition of your method. Here’s how that works:
The above code is essentially helping you to view the method as having multiple definitions as follows:
Note that we have removed the earlier definitions of the fullname() method and now we have a method that takes a generic second argument which is then unpacked inside a for loop and then concatenated into the output.
Here is some driver code:
Note that we have overloaded the fullname() method in all our invocations, once with one argument, once with no arguments, and finally with two arguments. The output is:
There is another way to do method overloading using Python decorators but that is beyond the scope of this post. Stay tuned for a future blogpost!
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.