Like other languages (for example method overloading in C++) do, python does not supports method overloading by default. But there are different ways to achieve method overloading in Python.
The problem with method overloading in Python is that we may overload the methods but can only use the latest defined method.
In the above code we have defined two product method, but we can only use the second product method, as python does not supports method overloading. We may define many method of same name and different argument but we can only use the latest defined method. Calling the other method will produce an error. Like here calling will produce an error as the latest defined product method takes three arguments.
Thus, to overcome the above problem we can use different ways to achieve the method overloading.
Method 1 (Not The Most Efficient Method):
We can use the arguments to make the same function work differently i.e. as per the arguments.
11 Hi Geeks
The problem with above code is that is makes code more complex with multiple if/else statement and is not the desired way to achieve the method overloading.
Method 2 (Efficient One):
By Using Multiple Dispatch Decorator
Multiple Dispatch Decorator Can be installed by:
pip3 install multipledispatch
In Backend, Dispatcher creates an object which stores different implementation and on runtime it selects the appropriate method as the type and number of parameters passed.
- Difference between Method Overloading and Method Overriding in Python
- Operator Overloading in Python
- Concatenate two strings using Operator Overloading in Python
- Function overloading with singledispatch-functools
- class method vs static method in Python
- Python | os.dup() method
- Python | next() method
- Python | set() method
- Python | Decimal exp() method
- Python PIL | ImagePalette() Method
- Python | sympy.nC() method
- Python PIL | putalpha() Method
- Python PIL | putdata() method
- Python PIL | getcolors() Method
- Python PIL | tobytes() Method
- Python PIL | getpalette() Method
- Python | os.WIFEXITED() method
- Python: filecmp.cmp() method
- Python - PyTorch log() method
- Python PIL | RankFilter() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Vidip