{"id":28233,"date":"2022-08-28T15:03:05","date_gmt":"2022-08-28T09:33:05","guid":{"rendered":"https:\/\/python-programs.com\/?p=28233"},"modified":"2022-08-28T15:03:05","modified_gmt":"2022-08-28T09:33:05","slug":"python-method-overloading","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-method-overloading\/","title":{"rendered":"Method Overloading in Python"},"content":{"rendered":"
In this article, let us look at Method Overloading in Python.<\/p>\n
Method Overloading is a form of Compile time polymorphism. In the case of method overloading, multiple methods belonging to the same class can have the same method name but different number of signatures\/ arguments.<\/p>\n
Method overloading can be used to add more to the behavior of the concerned methods. A user will not require more than one class to implement it.<\/p>\n
NOTE:<\/strong><\/p>\n Example<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Python does not support method overloading because Python always uses the last\u00a0defined<\/strong> method. We can still overload methods in Python, but it’s pointless. However, method overloading can be implemented in Java, C++, and other languages in the manner described above.<\/p>\n Another way to perform method overloading in Python is as follows:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Method overloading allows you to call the same method in multiple ways. All of the methods will have the same name, but there may be three differences:<\/p>\n In this article, let us look at Method Overloading in Python. Method Overloading: Method Overloading is a form of Compile time polymorphism. In the case of method overloading, multiple methods belonging to the same class can have the same method name but different number of signatures\/ arguments. Method overloading can be used to add more …<\/p>\nNote that Python does not support method overloading. A user may overload \r\nall of the methods, but they will only be able to use the\r\nlastest defined method.<\/pre>\n
\n
# Create a class say MethodOverloading\r\nclass MethodOverloading:\r\n # Create a function say multiply which accepts two numbers as arguments \r\n # and prints the multiplication result of those two numbers\r\n def multiply(self, x, y):\r\n print(x*y)\r\n # Create another function with the same name say multiply which accepts three \r\n # numbers as arguments and prints the multiplication result of those three numbers\r\n def multiply(self, x, y, z):\r\n print(x*y*z)\r\n\r\n# Create an object for the above MethodOverloading() class \r\nobj =MethodOverloading()\r\n# Pass two numbers as arguments to the multiply() function and apply it \r\n# on the above object and print the result\r\nprint(obj.multiply(2, 3))\r\n# Pass three numbers as arguments to the multiply() function and apply it \r\n# on the above object and print the result\r\nprint(obj.multiply(2, 3, 4))<\/pre>\n
---------------------------------------------------------------------------\r\nTypeError Traceback (most recent call last)\r\n<ipython-input-6-7d5007590dae> in <module>\r\n14 # Pass two numbers as arguments to the multiply() function and apply it\r\n15 # on the above object and print the result\r\n---> 16 print(obj.multiply(2, 3))\r\n17 # Pass three numbers as arguments to the multiply() function and apply it\r\n18 # on the above object and print the result\r\n\r\nTypeError: multiply() missing 1 required positional argument: 'z'<\/pre>\n
\n
# Create a class say MethodOverloading\r\nclass MethodOverloading:\r\n # Create a function say multiply which accepts two numbers as arguments \r\n # and prints the multiplication result of those two numbers based on conditions\r\n def multiply(self, a = None, b = None):\r\n # Check if both the numbers are not equal to None using the if conditional \r\n # statement, and operator\r\n if a!= None and b!= None:\r\n # If it is true then return the multiplication of both the numbers\r\n return a*b\r\n # else if Check only the first number passed is not None using the elif conditional statement\r\n elif a!= None:\r\n # If it is true then return only the first number\r\n return a\r\n else:\r\n # Else return 0\r\n return 0\r\n\r\n# Create an object for the above MethodOverloading() class \r\nclass_object = MethodOverloading()\r\n\r\n# Call the above multiply() function without passing any arguments and print the result\r\nprint(\"Result = \", class_object.multiply())\r\n# Call the above multiply() function by passing only one number as an argument and print the result\r\nprint(\"Result = \", class_object.multiply(3))\r\n# Call the above multiply() function by passing two numbers as arguments and print the result\r\nprint(\"Result = \", class_object.multiply(4, 5))<\/pre>\n
Result = 0\r\nResult = 3\r\nResult = 20<\/pre>\n
Advantages of Method overloading<\/h4>\n
\n
Method overloading In Brief<\/h4>\n
\n
\n