Monday, May 6, 2019

Method Overloading in Python

Method overloading is an OOPS concept which provides ability to have several methods having the same name with in the class where the methods differ in types or number of arguments passed.

Method overloading in Python

Method overloading in its traditional sense (as defined above) as exists in other languages like method overloading in Java doesn’t exist in Python.

In Python if you try to overload a function by having two or more functions having the same name but different number of arguments only the last defined function is recognized, calling any other overloaded function results in an error.

For example in the given class there are two methods having the name sum but number of parameters differ.

class OverloadDemo:
  # first sum method, two parameters
  def sum(self, a, b):
    s = a + b
    print(s)
  # overloaded sum method, three parameters
  def sum(self, a, b, c):
    s = a + b + c
    print(s)

od =  OverloadDemo()
od.sum(7, 8)

Output

File "F:/NETJS/NetJS_2017/Python/Test/OverloadDemo.py", line 10, in <module>
    od.sum(7, 8)
TypeError: sum() missing 1 required positional argument: 'c'

As you can here when calling the method with two parameters an error is thrown as this sum() method is not the latest.

If you call the method having three parameters code runs fine.

class OverloadDemo:
  # first sum method, two parameters
  def sum(self, a, b):
    s = a + b
    print(s)
  # overloaded sum method, three parameters
  def sum(self, a, b, c):
    s = a + b + c
    print(s)

od =  OverloadDemo()
od.sum(7, 8, 9)

Output

24

Process finished with exit code 0

Achieving method overloading in Python

Since using the same method name again to overload the method is not possible in Python, so achieving method overloading in Python is done by having a single method with several parameters. Then you need to check the actual number of arguments passed to the method and perform the operation accordingly.

For example if we take the same method sum() as used above with the capability to pass two or three parameters, in Python this method overloading can be done as shown in the below example.

class OverloadDemo:
  # sum method with default as None for parameters
  def sum(self, a=None, b=None, c=None):
    # When three params are passed
    if a!=None and b!=None and c!=None:
      s = a + b + c
      print('Sum = ', s)
    # When two params are passed
    elif a!=None and b!=None:
      s = a + b
      print('Sum = ', s)

od =  OverloadDemo()
od.sum(7, 8)
od.sum(7, 8, 9)

Output

Sum =  15
Sum =  24

Process finished with exit code 0

That's all for this topic Method Overloading in Python. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Python Tutorial Page


Related Topics

  1. Python Installation on Windows
  2. Python First Program - Hello World
  3. Encapsulation in Python
  4. Name Mangling in Python
  5. Namespace And Variable Scope in Python

You may also like-

  1. Check String Empty or Not in Python
  2. Abstract Class in Python
  3. Concatenating Lists in Python
  4. Python Generator, Generator Expression, Yield Statement
  5. Marker interface in Java
  6. How Linked List class works internally in Java
  7. Race Condition in Java Multi-Threading
  8. What is Hadoop Distributed File System (HDFS)