Why function overloading in Python doesn’t works meanwhile overriding is okay?


This is basic question that frequently asked by a lot of people who learning Python. Basically, we know there are two method when calling function inside class :

1. Method Overriding
This function overiding allow Child or derived class to overwrite function in Parent class by define same function. This is common thing in inheritance method in Python. For example:

1
2
3
4
5
6
7
8
9
10
11
class Base(object):
    def show_status(self):
        print "Hello World"

class Child(Base):
    def show_status(self):
        print "Children replaced it!"

if __name__ == "__main__":
    data = Child()
    data.show_status()

When you’re running this code, it will showing results “Children replaced it”. This is have mean that show_status() function in Child() is overriding the same method name in Base().

2. Method Overloading
This method allowing Class have same class but showing different results / method based on signature or given arguments. For example, you have Base class. You want show_status() have different behaviour when we put several input on it.

1
2
3
4
5
6
7
8
9
10
class Base(object):
    def show_status(self):
        print "Hello World"
     
    def show_status(self, speed=None):
        print("Your speed now : %s" % speed)

if __name__ == "__main__":
    data = Base()
    data.show_status()

When you executing this scripts, what the result you want to expect? “Hello World” or “Your speed now : None” ?

Well, you will find the second one as the results. If function overloading is working in Python, then the results :

1. We run data.show_status()
It should running the first one method in Base(). That’s mean it should showing results “Hello World”

2. We run data.show_status(speed=1000)
It will executed the second function and return “Your speed now : 100”

If you try this on Java, you will found that this method overloading is working, eg :

BaseExample.java :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Base {
    void show_status() {
        System.out.println("Hello World");
    }

    void show_status(int speed){
        System.out.println("Your speed is " + speed);
    }
}

public class BaseExample {
    public static void main(String args[]) {
        Base obj = new Base();

        // Without argument
        obj.show_status();

        // With argument
        obj.show_status(100);
    }
}

Compile and executing this file by :

1
2
javac BaseExample.java
java BaseExample

You will get results :

1
2
Hello World
Your speed is 100

But why in static typing method overloading is working meanwhile not in Python (duck typing) ?

The answer is “virtual function” ( http://en.wikipedia.org/wiki/Virtual_function ). All function / method in Python is virtual function, which it have mean that derived class can overwrite method in base class using the same name. This is the reason why we can’t apply method overloading in Python.

But, one last try, how we can call method in base class ? Well, “Super” (http://docs.python.org/library/functions.html) comes to give you rescue. You can calling method in base class by :

1
2
3
4
5
6
7
8
9
10
11
12
class Base(object):
    def show_status(self):
        print "Hello World"

class Child(Base):
    def show_status(self):
        print "Children replaced it!"

if __name__ == "__main__":
    data = Child()
    data.show_status()
    super(Child, data).show_status()

Now you know what Python can’t use method overloading but at least we still can calling method in Base class anyway 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.