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

Share
Published by
Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.