Blog

Blog

What is Method Overloading in Python and How it Works?

What is Method Overloading in Python and How it Works?

Youtube banner Logo

Method Overloading in Python

Method overloading in Python is a technique that allows a class to have multiple methods with the same name but different arguments. When a method is called, Python automatically selects the correct version of the method based on the number and type of arguments passed to the method.

Method overloading is not a built-in feature in Python, unlike some other programming languages like Java and C++. However, it can be achieved in Python by using default arguments, variable-length arguments, and function annotations.

Here’s an example of how to use default arguments to achieve method overloading:

class MyClass:
    def my_method(self, arg1, arg2=None):
        if arg2 is None:
            print("You passed one argument:", arg1)
        else:
            print("You passed two arguments:", arg1, arg2)
obj = MyClass()
obj.my_method(1)
obj.my_method(1, 2

In this example, the my_method has two different versions, one that takes one argument and another that takes two arguments. When obj.my_method(1) is called, Python automatically selects the version of the method that takes one argument and assigns the value None to the second argument.

Similarly, when obj.my_method(1, 2) is called, Python selects the version of the method that takes two arguments and assigns the values 1 and 2 to the first and second arguments, respectively.

Another way to achieve method overloading in Python is by using variable-length arguments. Here’s an example:

class MyClass:
    def my_method(self, *args):
        if len(args) == 1:
            print("You passed one argument:", args[0])
        elif len(args) == 2:
            print("You passed two arguments:", args[0], args[1])
        else:
            print("You passed more than two arguments.")
obj = MyClass()
obj.my_method(1)
obj.my_method(1, 2)
obj.my_method(1, 2, 3, 4

In this example, the my_method takes a variable-length argument, *args, which allows it to accept any number of arguments. The method then uses the len(args) function to determine the number of arguments passed to it and selects the appropriate code to execute based on that number.

Lastly, Python 3.5 and later versions added support for function annotations, which allows you to specify the types of arguments and return values for a function.

Here’s an example of how to use function annotations to achieve method overloading:

class MyClass:
    def my_method(self, arg1: int, arg2: int = None) -> None:
        if arg2 is None:
            print("You passed one argument:", arg1)
        else:
            print("You passed two arguments:", arg1, arg2)
obj = MyClass()
obj.my_method(1)
obj.my_method(1, 2

In this example, the my_method takes two arguments, arg1 and arg2, and specifies their types using the : int and : int = None annotations, respectively. The -> None annotation specifies that the method does not return any value.

In summary, Method overloading in Python is a technique that allows a class to have multiple methods with the same name but different arguments. It can be achieved in Python by using default arguments, variable

FAQ’s

Q1 How can I implement method overloading in Python?

One way to implement method overloading in Python is to use default arguments. Another way is to use type checking, where the method checks the type of the arguments passed and behaves accordingly

Q2 What is the difference between method overloading and method overriding in Python?

Method overloading is the ability to define multiple methods with the same name in a class, but with different arguments. Method overriding is the ability to change the implementation of a method inherited from a parent class in a child class.

Q3 Is method overloading supported in Python?

No, Python does not have built-in support for method overloading.

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare

Subscribe to Newsletter

Stay ahead of the rapidly evolving world of technology with our news letters. Subscribe now!