Method Overloading in Python With Example

If you are already familiar with the concept of method overloading in Python from some other programming language, then you would know that with method overloading, we do have different methods in the class with the same name. This is well and good, but in python, we cannot simply perform method overloading using the same method name. Even if you try it out yourselves, you are going to find something unusual in comparison to some other programming languages that might be known to you.

The reason for this is that everything in python is an object, which means that the class that you are creating is also an object, or the method that you are creating is also an object.

Method Overloading in Python

Well, this might be confusing, but you need to understand this so that you can get familiar with the concept.

For this, Let’s understand what happens when we write some method in the class(or in general a function as well). When we write a method, we have created an object, which is a method, and it is assigned to the attribute which is the name of the method. So, if you are again defining the method with the same name, the second definition would just replace the first one, which is simply how the assignment usually behaves.

So, Let’s have a super simple example, which explains the same thing –

As you can see in the above program, we have a class GyaniPandit, and inside the class, we have created a method, with the name sayGoodMorning. Now, it does not accept any arguments, and we are again writing the method in the same class, with the same name, and it is also not accepting any arguments, but still, we do not get any error. Instead, when we are creating the object, and then calling the method say good morning, the latest definition that we have written executes, which is of no surprise now, since now we know, that everything in python is an object, even the method.

Also, if you now try to change the parameters that the methods take, and again keep the same name, this is also not going to work the way we want. Have a look at this –

As you can see, we again have a similar example, with a few modifications. We just have written the sayGoodMorning method again, but this time we have changed the parameters. But here, again it is going to go with the latest definition, and when we are creating the object, and executing the method again, we are going to get an error, since the method that we are trying to call is not there. You can try calling it though.

Now, instead of doing what we have been doing till now, we can create a single method, that takes multiple parameters. Here is how we are going to do it.

As you can see, we have written the method sayGoodMorning, and it basically takes in an additional argument, which is the name, and we have also given the default value as None so that we do not run into an error if we do not pass the argument. After that, in the method, we are checking that if the name is not any, we are printing good morning, with the name, and else, we only print good morning.

If you wish, you can add some additional parameters there as well, but accordingly, you will have to do the necessary checks, if you are taking in the firstname and the lastname as parameters, then it would be something like this –

As you can see, in the sayGoodMorning method, this time, we are taking in the firstname and the lastname as well. The default value for both of them is set to None. After that, in the method, we are checking that if the firstname and lastname are None, we are printing good morning with both, else we are again checking that if the user has passed only one argument, and if yes, we are printing good morning with only firstname, and else we are printing only good morning.

This way, we can call the method sayGoodMorning, with different parameters. This can be considered as a way to achieve method overloading in this case. We are not writing multiple methods of the same name though.

You can practice it, and try doing some more examples so that you can get familiar with the concept.