Categories: Oop pythonPython

Method overloading in Python language

Method overloading in Python language

In this tutorial, we will discuss  Method overloading in Python language

Method overloading

Method overloading is a OOP concept in Python language

Method overloading Is not supported in Python, except in some situations. In case, you overload methods then you will be able to access the last defined method.

we can define more than one methods in the same name and we can call that many ways with Zero, one, or more parameters but you can access only last defined method in python.

Method overloading concept

overloading is done by changing the data type of variable, number of variable and passing to the parameter list.

When we call that method, we can make the following changes

1. Empty parameter

2. different number of parameter

3. different type of parameter

above diagram shows how to call a method in various ways, x,y,z are different data type to pass parameter. When we call that method, we can use that data type in a different way in OOP’s languages

 

Method in Python

Already we have learned python methods but, in this article, I like to look back at it for you

Method  – Exmples

class SumNum:

    def add(self,x,y,z):
        return x+y+z

calc=SumNum()

print(calc.add(2,3,4))
print(calc.add(7,5,8))
print(calc.add('a','b','c'))
print(calc.add('b','c','d'))
print(calc.add(4.5,7.3,5.9))

When the above code is executed, The following output is displayed

9
20
abc
bcd
17.7
although the program similar to method overloading but it is no method overloading. In the above program, we have defined only one method and call it. output displayed here

Method oveloading in Python

“If” method overloading is not possible in python but python allows access to last definition method. If not, when we run the module in python, an error message is generated.
Program 2
Call early definition of the method but it not supported
This is an example of that
When the following code is executed, The following output is displayed
Error massage - TypeError: add() takes exactly 3 arguments (2 given)
This is because Python understands the latest definition of method add() which takes only three arguments. which is not allow two arguments (earlier definition).

Program 3

class SumNum:
    def add(self,x,y):
        return x+y

    def add(self,x,y,z):
        return x+y+z

calc=SumNum()

print(calc.add(4,7,5))
#print(calc.add(7,5))
print(calc.add('a','b','c'))
print(calc.add(4.5,7.3,5.9))

When the following code is executed, The following output is displayed

16
abc
17.7

In the above program, python allows access to the last definition method with three arguments. Second statements have two arguments but it is the comment here.

 

Program 4

class SumNum:
    def add(self,x,y):
        return x+y

    def add(self,x,y,z):
        return x+y+z

calc=SumNum()

print(calc.add(4,7,5))
print(calc.add(7,5))
print(calc.add('a','b','c'))
print(calc.add(4.5,7.3,5.9))

When the following code is executed, The following output is displayed

16

Traceback (most recent call last):
File "C:/Python27/calcadd.py", line 11, in <module>
print(calc.add(7,5))
TypeError: add() takes exactly 4 arguments (3 given)

Let’s see this simple example of the method over loading

In this example, we have two methods which have the same names but the different number of the argument list. If you know java or c++(Oop languages) we will find nothing wrong this scope. But python only understand  latest implements in the add method

print(maths.add(4,7,5))  //if we call add method with three arguments python runs successful
Generate output -16

print(maths.add(7,5))   //if we call add method with two arguments python is not understand.So generate error massage

Program 4

When the following code is executed, The following output is displayed

Hello
hello jhon
Suggested for you
Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.