How do I use method overloading in Python?

PythonClassOverloading

Python Problem Overview


I am trying to implement method overloading in Python:

class A:
    def stackoverflow(self):  	
        print 'first method'
    def stackoverflow(self, i):
        print 'second method', i

ob=A()
ob.stackoverflow(2)

but the output is second method 2; similarly:

class A:
    def stackoverflow(self):  	
        print 'first method'
    def stackoverflow(self, i):
        print 'second method', i

ob=A()
ob.stackoverflow()

gives

Traceback (most recent call last):
  File "my.py", line 9, in <module>
    ob.stackoverflow()
TypeError: stackoverflow() takes exactly 2 arguments (1 given)

How do I make this work?

Python Solutions


Solution 1 - Python

It's method overloading, not method overriding. And in Python, you historically do it all in one function:

class A:
    def stackoverflow(self, i='some_default_value'):
        print 'only method'

ob=A()
ob.stackoverflow(2)
ob.stackoverflow()

See the Default Argument Values section of the Python tutorial. See https://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument for a common mistake to avoid.

See PEP 443 for information about the single dispatch generic functions added in Python 3.4:

>>> from functools import singledispatch
>>> @singledispatch
... def fun(arg, verbose=False):
...     if verbose:
...         print("Let me just say,", end=" ")
...     print(arg)
>>> @fun.register(int)
... def _(arg, verbose=False):
...     if verbose:
...         print("Strength in numbers, eh?", end=" ")
...     print(arg)
...
>>> @fun.register(list)
... def _(arg, verbose=False):
...     if verbose:
...         print("Enumerate this:")
...     for i, elem in enumerate(arg):
...         print(i, elem)

Solution 2 - Python

You can also use pythonlangutil:

from pythonlangutil.overload import Overload, signature

class A:
    @Overload
    @signature()
    def stackoverflow(self):    
        print 'first method'
    
    @stackoverflow.overload
    @signature("int")
    def stackoverflow(self, i):
        print 'second method', i

Solution 3 - Python

While agf was right with the answer in the past, pre-3.4, now with PEP-3124 we got our syntactic sugar.

See typing documentation for details on the @overload decorator, but note that this is really just syntactic sugar and IMHO this is all people have been arguing about ever since.

Personally, I agree that having multiple functions with different signatures makes it more readable then having a single function with 20+ arguments all set to a default value (None most of the time) and then having to fiddle around using endless if, elif, else chains to find out what the caller actually wants our function to do with the provided set of arguments. This was long overdue following the Python Zen:

> Beautiful is better than ugly.

and arguably also

> Simple is better than complex.

Straight from the official Python documentation linked above:

from typing import overload
@overload
def process(response: None) -> None:
	...
@overload
def process(response: int) -> Tuple[int, str]:
	...
@overload
def process(response: bytes) -> str:
	...
def process(response):
	<actual implementation>

EDIT: for anyone wondering why this example is not working as you'd expect if from other languages I'd suggest to take a look at this discussion. The @overloaded functions are not supposed to have any actual implementation. This is not obvious from the example in the Python documentation.

Solution 4 - Python

In Python, you don't do things that way. When people do that in languages like Java, they generally want a default value (if they don't, they generally want a method with a different name). So, in Python, you can have default values.

class A(object):  # Remember the ``object`` bit when working in Python 2.x

    def stackoverflow(self, i=None):
        if i is None:
            print 'first form'
        else:
            print 'second form'

As you can see, you can use this to trigger separate behaviour rather than merely having a default value.

>>> ob = A()
>>> ob.stackoverflow()
first form
>>> ob.stackoverflow(2)
second form

Solution 5 - Python

You can't, never need to and don't really want to.

In Python, everything is an object. Classes are things, so they are objects. So are methods.

There is an object called A which is a class. It has an attribute called stackoverflow. It can only have one such attribute.

When you write def stackoverflow(...): ..., what happens is that you create an object which is the method, and assign it to the stackoverflow attribute of A. If you write two definitions, the second one replaces the first, the same way that assignment always behaves.

You furthermore do not want to write code that does the wilder of the sorts of things that overloading is sometimes used for. That's not how the language works.

Instead of trying to define a separate function for each type of thing you could be given (which makes little sense since you don't specify types for function parameters anyway), stop worrying about what things are and start thinking about what they can do.

You not only can't write a separate one to handle a tuple vs. a list, but also don't want or need to.

All you do is take advantage of the fact that they are both, for example, iterable (i.e. you can write for element in container:). (The fact that they aren't directly related by inheritance is irrelevant.)

Solution 6 - Python

I think the word you're looking for is "overloading". There isn't any method overloading in Python. You can however use default arguments, as follows.

def stackoverflow(self, i=None):
    if i != None:
        print 'second method', i
    else:
        print 'first method'

When you pass it an argument, it will follow the logic of the first condition and execute the first print statement. When you pass it no arguments, it will go into the else condition and execute the second print statement.

Solution 7 - Python

I write my answer in Python 3.2.1.

def overload(*functions):
    return lambda *args, **kwargs: functions[len(args)](*args, **kwargs)

How it works:

  1. overload takes any amount of callables and stores them in tuple functions, then returns lambda.
  2. The lambda takes any amount of arguments, then returns result of calling function stored in functions[number_of_unnamed_args_passed] called with arguments passed to the lambda.

Usage:

class A:
    stackoverflow=overload(                    \
        None, \ 
        #there is always a self argument, so this should never get called
        lambda self: print('First method'),      \
        lambda self, i: print('Second method', i) \
    )

Solution 8 - Python

I write my answer in Python 2.7:

In Python, method overloading is not possible; if you really want access the same function with different features, I suggest you to go for method overriding.

class Base(): # Base class
    '''def add(self,a,b):
        s=a+b
        print s'''

    def add(self,a,b,c):
        self.a=a
        self.b=b
        self.c=c
        
        sum =a+b+c
        print sum
        
class Derived(Base): # Derived class
    def add(self,a,b): # overriding method
        sum=a+b
        print sum
    


add_fun_1=Base() #instance creation for Base class
add_fun_2=Derived()#instance creation for Derived class

add_fun_1.add(4,2,5) # function with 3 arguments
add_fun_2.add(4,2)   # function with 2 arguments

Solution 9 - Python

In Python, overloading is not an applied concept. However, if you are trying to create a case where, for instance, you want one initializer to be performed if passed an argument of type foo and another initializer for an argument of type bar then, since everything in Python is handled as object, you can check the name of the passed object's class type and write conditional handling based on that.

class A:
   def __init__(self, arg)
      # Get the Argument's class type as a String
      argClass = arg.__class__.__name__
     
      if argClass == 'foo':
         print 'Arg is of type "foo"'
         ...
      elif argClass == 'bar':
         print 'Arg is of type "bar"'
         ...
      else
         print 'Arg is of a different type'
         ...

This concept can be applied to multiple different scenarios through different methods as needed.

Solution 10 - Python

In Python, you'd do this with a default argument.

class A:

    def stackoverflow(self, i=None):    
        if i == None:
            print 'first method'
        else:
            print 'second method',i

Solution 11 - Python

I just came across overloading.py (function overloading for Python 3) for anybody who may be interested.

From the linked repository's README file:

> overloading is a module that provides function dispatching based on > the types and number of runtime arguments. > > When an overloaded function is invoked, the dispatcher compares the > supplied arguments to available function signatures and calls the > implementation that provides the most accurate match. > > Features > > Function validation upon registration and detailed resolution rules > guarantee a unique, well-defined outcome at runtime. Implements > function resolution caching for great performance. Supports optional > parameters (default values) in function signatures. Evaluates both > positional and keyword arguments when resolving the best match. > Supports fallback functions and execution of shared code. Supports > argument polymorphism. Supports classes and inheritance, including > classmethods and staticmethods.

Solution 12 - Python

Python does not support method overloading like Java or C++. We may overload the methods, but we can only use the latest defined method.

# First sum method.
# Takes two argument and print their sum
def sum(a, b):
    s = a + b
    print(s)

# Second sum method
# Takes three argument and print their sum
def sum(a, b, c):
    s = a + b + c
    print(s)

# Uncommenting the below line shows an error
# sum(4, 5)

# This line will call the second sum method
sum(4, 5, 5)

We need to provide optional arguments or *args in order to provide a different number of arguments on calling.

Courtesy Python | Method Overloading

Solution 13 - Python

Python 3.x includes standard typing library which allows for method overloading with the use of @overload decorator. Unfortunately, this is to make the code more readable, as the @overload decorated methods will need to be followed by a non-decorated method that handles different arguments. More can be found here here but for your example:

from typing import overload
from typing import Any, Optional
class A(object):
    @overload
    def stackoverflow(self) -> None:    
        print('first method')
    @overload
    def stackoverflow(self, i: Any) -> None:
        print('second method', i)
    def stackoverflow(self, i: Optional[Any] = None) -> None:
        if not i:
            print('first method')
        else:
            print('second method', i)

ob=A()
ob.stackoverflow(2)

Solution 14 - Python

In the MathMethod.py file:

from multipledispatch import dispatch
@dispatch(int, int)
def Add(a, b):
   return a + b 
@dispatch(int, int, int)  
def Add(a, b, c):
   return a + b + c 
@dispatch(int, int, int, int)    
def Add(a, b, c, d):
   return a + b + c + d

In the Main.py file

import MathMethod as MM 
print(MM.Add(200, 1000, 1000, 200))

We can overload the method by using multipledispatch.

Solution 15 - Python

Python added the @overload decorator with PEP-3124 to provide syntactic sugar for overloading via type inspection - instead of just working with overwriting.

Code example on overloading via @overload from PEP-3124

from overloading import overload
from collections import Iterable

def flatten(ob):
    """Flatten an object to its component iterables"""
    yield ob

@overload
def flatten(ob: Iterable):
    for o in ob:
        for ob in flatten(o):
            yield ob

@overload
def flatten(ob: basestring):
    yield ob

is transformed by the @overload-decorator to:

def flatten(ob):
    if isinstance(ob, basestring) or not isinstance(ob, Iterable):
        yield ob
    else:
        for o in ob:
            for ob in flatten(o):
                yield ob

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1335578View Question on Stackoverflow
Solution 1 - PythonagfView Answer on Stackoverflow
Solution 2 - PythonEhsan KeshavarzianView Answer on Stackoverflow
Solution 3 - PythonomniView Answer on Stackoverflow
Solution 4 - PythonChris MorganView Answer on Stackoverflow
Solution 5 - PythonKarl KnechtelView Answer on Stackoverflow
Solution 6 - PythonmayhewrView Answer on Stackoverflow
Solution 7 - PythonGingerPlusPlusView Answer on Stackoverflow
Solution 8 - PythonMoorthi MuthuView Answer on Stackoverflow
Solution 9 - PythonS. GamgeeView Answer on Stackoverflow
Solution 10 - PythonJohn Gaines Jr.View Answer on Stackoverflow
Solution 11 - PythonMark LawrenceView Answer on Stackoverflow
Solution 12 - PythonAtihskaView Answer on Stackoverflow
Solution 13 - PythonnickthefreakView Answer on Stackoverflow
Solution 14 - PythonMahabubuzzamanView Answer on Stackoverflow
Solution 15 - PythonNexView Answer on Stackoverflow