The-correct-way-to-overload-functions-in-PYTHON..jpg

The correct way to overload functions in PYTHON.

Before going further let’s learn what is Method overloading?

Method overloading is a salient feature of Polymorphism in Object-Oriented Programming (OOP). It lets you declare the same method multiple times with different argument lists.

 

Method overloading is a form of polymorphism in OOP. 

Polymorphism allows objects or methods to act in different ways, according to the means in which they are used. One such manner in which the methods behave according to their argument types and number of arguments is method overloading. 

Well there are some advantages using method overloading but like other languages (for example, method overloading in java,C++ and e.t.c) do, python does not support method overloading by default.

Python does not support method overloading, that is, it is not possible to define more than one method with the same name in a class in python.

This is because method arguments in python do not have a type. A method accepting one argument can be called with an integer value, a string or a double.


Consider code sample below.

Same method works for 3 different data types.
Thus, you cannot define two methods with the same name and same number of arguments but having different type as shown in the above example.  They will be treated as the same method.

In simple words Python accepts when arguments are different types.

Can overloading be performed when the number of arguments are different?

Let’s try what happens if we do method overloading in python?

In the above programme as you can see a simple class is created with multiple methods with same name and obj is the object for the class Method_overloading.

Lets call the methods inside the class.

Above code when executed will result in an error.


But when we are passing three arguments then we are getting output.

This is because the last method overwrites the other two methods. python treats all the methods with the same name as one method irrespective of the number of arguments.

If there are more than one methods with same name then last method definition will over write the other even if they have different number of arguments.

Is there any alternative way to do method overloading in python?

Answer is yes. There are some ways to do method overloading in python.

Let’s look at some example for method overloading.


Method Overloading Examples

 Using default argument values:

It is possible to provide default values to method arguments while defining a method. If method arguments are supplied default values, then it is not mandatory to supply those arguments while calling method. Python considers the values of those arguments while executing the method. 

Example – 

Above class defines a method with 2 arguments having default values and it is called without any arguments. Python utilizes those default values while executing the method.

It is not necessary to provide default values to all the arguments but if an argument is supplied a default value, then all the arguments towards its right should be supplied default values.

If argument values are supplied while calling the method then those values overwrite the default values as shown in below example –


But the problems is we are using default values to call method with different argument numbers.

Assigning a value of None:

Let’s take an example. Here, we create a class with one method sum(). The parameters of this method is set to None. This will give us the option to call it with or without a parameter.


To clarify method overloading, we can now call the method sum() in different ways:

obj.sum(7,8,9)
obj.sum(7,8)
obj.sum(7)
obj.sum()

Above class contains a method with 3 arguments having default values as None. Default value of None means argument has not been explicitly provided a value.

Sum() method that can be called with fewer arguments than it is defined to allow. Also, it is not limited to three variables and your method can have more variables which are optional.

-Using variable arguments:

An asterisk before an argument lets you supply 0 or more arguments to a method.

Lets see an example

Notice the method defines only one argument with asterisk (*a).

This lets you supply any number of arguments to it without any errors. This method is then called with different number of arguments producing the output.

When a method contains an argument preceded with a *, it accepts the values as a tuple internally.

As clear from above code, variable arguments lets you achieve method overloading in that you can have methods that accept different number of arguments but with only one method.

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