Skip to content

Instantly share code, notes, and snippets.

@vikas-git
Last active March 2, 2020 18:09
Show Gist options
  • Save vikas-git/90922002a381afb172cc5f1d1a3bb6f4 to your computer and use it in GitHub Desktop.
Save vikas-git/90922002a381afb172cc5f1d1a3bb6f4 to your computer and use it in GitHub Desktop.
This gist is used only for find some internal functionality questions in python lang.

Python Important topics

Click ⭐if you like the project. Pull Request are highly appreciated.

Table of Contents

No. Questions
1 Why python is not support Overloading concept?
  1. Why python is not support Overloading concept?

Ref article: https://codippa.com/how-to-perform-method-overloading-in-python/

Python does not support method overloading, that is, it is not possible to define more than one method with the same name in a class in python. This is because method arguments in python do not have a type. A method accepting one argument can be called with an integer value, a string or a double. Consider code sample below.

class OverloadDemo:

    # define single argument method
    def method(self, a):
        print(a)

# create object of class
obj = OverloadDemo()
# call method with integer argument
obj.method(5) # 5
# call method with string argument
obj.method('codippa') # codippa
# call method with double argument
obj.method(10.2) # 10.2

Same method works for 3 different data types. Thus, you cannot define two methods with the same name and same number of arguments but having different type as shown in the above example. They will be treated as the same method.

Above example demonstrated the case when the arguments had different types but one might ask that overloading can also be performed when the number of arguments are different. This is also not possible because python treats all the methods with the same name as one method irrespective of the number of arguments. If there are more than one methods with same name then last method definition will over write the other even if they have different number of arguments, that is, python will only consider and the last method as if this is the only method present in the class as shown in below example.

class OverloadDemo:
 
    # define no argument method
    def method(self):
        print("No argument")
 
    # define 1 argument method
    def method(self, a):
        print("One argument")
 
    # define 2 argument method
    def method(self,a, b):
        print("Two arguments")
 
# create object of class
obj = OverloadDemo()
# call method with a single argument
obj.method(10)

Above code when executed will result in an error as

TypeError: method() missing 1 required positional argument: ‘b

This is because the last method over writes the other two methods and only one method is present in the class which accepts two arguments but we supplied only 1 argument while calling. If the number of arguments of the last method do not match the number of arguments supplied while calling the method, it will be an error.

⬆ Back to Top

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment