Teachnique
      CourseRoadmaps
      Login

      OverviewCommentsUser InputNumbersBooleansHistoryHello World ProgramEnvironment SetupSyntaxVariablesData TypesType CastingUnicode SystemLiteralsOperators

      Control FlowBreak StatementContinue StatementPass StatementNested LoopsDecision MakingIf StatementIf-else StatementNested IF StatementMatch-Case StatementLoopsFor LoopsFor-else LoopsWhile Loops

      FunctionsBuilt-in FunctionsDefault ArgumentsKeyword ArgumentsKeyword-Only ArgumentsPositional ArgumentsPositional-Only ArgumentsArbitrary ArgumentsVariable ScopeFunction AnnotationsModules

      StringSlicing StringsModify StringsString ConcatenationString FormattingEscape CharactersString MethodsString Exercises

      ListsList ExercisesAccess List ItemsChange List ItemsAdd List ItemsRemove List ItemsLoop ListsList ComprehensionSort ListsCopy ListsJoin ListsList Methods

      TuplesAccess Tuple ItemsUpdate TuplesUnpack Tuple ItemsLoop TuplesJoin TuplesTuple MethodsTuple Exercises

      SetsAccess Set ItemsAdd Set ItemsRemove Set ItemsLoop SetsJoin SetsCopy SetsSet OperatorsSet MethodsSet Exercises

      DictionariesDictionary ExercisesAccess Dictionary ItemsChange Dictionary ItemsAdd Dictionary ItemsRemove Dictionary ItemsDictionary View ObjectsLoop DictionariesCopy DictionariesNested DictionariesDictionary Methods

      ArraysAccess Array ItemsAdd Array ItemsRemove Array ItemsLoop ArraysCopy ArraysReverse ArraysSort ArraysJoin ArraysArray MethodsArray Exercises

      File HandlingWrite to FileRead FilesRenaming and Deleting FilesDirectoriesFile Methods

      OOP ConceptsDynamic BindingDynamic TypingAbstractionObject and ClassesEncapsulationInterfacesPackagesInner ClassesAnonymous Class and ObjectsSingleton ClassWrapper ClassesEnumsReflectionClass AttributesClass MethodsStatic MethodsConstructorsAccess ModifiersInheritancePolymorphismMethod OverridingMethod Overloading

      Feedback

      Submit request if you have any questions.

      Course
      Method Overloading

      Python Tutorial

      This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.

      Method Overloading

      Method overloading is an important feature of object-oriented programming. Java, C++, C# languages support method overloading, but in Python it is not possible to perform method overloading.
      When you have a class with method of one name defined more than one but with different argument types and/or return type, it is a case of method overloading. Python doesn't support this mechanism as the following code shows

      Example

      class example:
      def add(self, a, b):
      x = a+b
      return x
      def add(self, a, b, c):
      x = a+b+c
      return x
      
      obj = example()
      
      print (obj.add(10,20,30))
      print (obj.add(10,20))

      Output

      The first call to add() method with three arguments is successful. However, calling add() method with two arguments as defined in the class fails.
      60
      Traceback (most recent call last):
      File "C:\Users\user\example.py", line 12, in <module>
      print (obj.add(10,20))
      ^^^^^^^^^^^^^^
      TypeError: example.add() missing 1 required positional argument: 'c'
      The output tells you that Python considers only the latest definition of add() method, discarding the earlier definitions.
      To simulate method overloading, we can use a workaround by defining default value to method arguments as None, so that it can be used with one, two or three arguments.

      Example

      class example:
      def add(self, a = None, b = None, c = None):
      x=0
      if a !=None and b != None and c != None:
      x = a+b+c
      elif a !=None and b != None and c == None:
      x = a+b
      return x
      
      obj = example()
      
      print (obj.add(10,20,30))
      print (obj.add(10,20))
      It will produce the following output
      60
      30
      With this workaround, we are able to incorporate method overloading in Python class.
      Python's standard library doesn't have any other provision for implementing method overloading. However, we can use dispatch function from a third party module named MultipleDispatch for this purpose.
      First, you need to install the Multipledispatch module.
      pip install multipledispatch
      This module has a @dispatch decorator. It takes the number of arguments to be passed to the method to be overloaded. Define multiple copies of add() method with @dispatch decorator as below

      Example

      from multipledispatch import dispatch
      class example:
      @dispatch(int, int)
      def add(self, a, b):
      x = a+b
      return x
      @dispatch(int, int, int)
      def add(self, a, b, c):
      x = a+b+c
      return x
      
      obj = example()
      
      print (obj.add(10,20,30))
      print (obj.add(10,20))

      Output

      60
      30