Overloading in Python

 

Python Overloading

Overloading is nothing, but rewriting the two functions with same name and different parameters. 

  • Rules to override
    • Inheritance is not mandatory
    • Method name must be same
    • No. of parameters should be different (Must have different signature).

  • Python does not support function / method overloading.
      • May overload the methods, but can only use the latest defined method 

        • Overloading in different class via imheritance

    #program for method overloading in different class via inheritance
    class A :
    def __init__(self):
    print("This is class A constructor")

    def add(self, a, b):
    print("add function of class A")
    return a+b;

    class B(A) :
    def add(self, a, b, c):
    print("add function of class B")
    return a-b;


    b = B()
    d = b.add(
    10, 5)
    print("d :", d)

     #Output

    This is class A constructor
    Traceback (most recent call last):
    File "
    C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 10, in <module>
    d = b.add(10, 5)
    TypeError: add() missing 1 required positional argument: 'c
    '


        • Overloading in same class

    #program for method overloading in same class

    class A :
    def __init__(self):
    print("This is class A constructor")

    def add(self, a, b):
    print("add function of class A")
    return a+b;

    def add(self, a, b, c):
    print("add function of class B")
    return a-b;



    b = A()
    d = b.add(
    10, 5)
    print("d :", d)

     #Output

    This is class A constructor
    Traceback (most recent call last):
    File "
    C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 18, in <module>
    d = b.add(10, 5)
    TypeError: add() missing 1 required positional argument: 'c'



    So above in both the cases, call is happening to latest defined function if we have 2 functions with same name, lets reverse function definition and see


    #program

    class A :
    def __init__(self):
    print("This is class A constructor")

    def add(self, a, b, c):
    print("add function of class A")
    return a+b;

    def add(self, a, b,):
    print("add function of class B")
    return a-b;



    b = A()
    d = b.add(
    10, 5)
    print("d :", d)

     #Output

    This is class A constructor
    add function of
    class B
    d :
    5


      • May overload the function, but can only use the latest defined method

    #program for function overloading

    def add(a, b,):
    print("add function with 2 parameter")
    return a+b;

    def add(a, b, c):
    print("add function with 3 parameter")
    return a-b+c;

    d = add(
    10, 5)
    print("d :", d)


    #output

    Traceback (most recent call last):
    File "
    C:/Self_Practice_Er_M_S_Dandyan/Python/Pycharm/xml/main.py", line 11, in <module>
    d = add(10, 5)
    TypeError: add() missing 1 required positional argument: 'c'



    Lets try with changing the definition order

    #program for constructor overriding

    def add(a, b, c):
    print("add function with 3 parameter")
    return a+b+c;

    def add(a, b,):
    print("add function with 2 parameter")
    return a-b;

    d = add(
    10, 5)
    print("d :", d)

     #Output

    add function with 2 parameter
    d :
    5



    So above example confirms, that we will not get any error of defining function / method as per rules of Overloading, but call will happen to latest defined function / method always



    Comments

    My photo
    Techno Xpresss
    Bangalore, Karnataka, India