Overloaded functions in python?

Is it possible to have overloaded functions in Python? In C #, I would do something like

void myfunction (int first, string second) { //some code } void myfunction (int first, string second , float third) { //some different code } 

and then when I call the function, it will distinguish two of them depending on the number of arguments. Is it possible to do something like this in Python?

+58
function python overloading arguments
Aug 18 '11 at 19:31
source share
5 answers

EDIT . For new generic submit functions in Python 3.4 see http://www.python.org/dev/peps/pep-0443/

You don't need to overload functions in Python at all. Python is dynamically typed and supports optional arguments to functions.

 def myfunction(first, second, third = None): if third is None: #just use first and second else: #use all three myfunction(1, 2) # third will be None, so enter the 'if' clause myfunction(3, 4, 5) # third isn't None, it 5, so enter the 'else' clause 
+74
Aug 18 '11 at 19:33
source share

in ordinary python you cannot do what you want. There are two close approximations:

 def myfunction(first, second, *args): # args is a tuple of extra arguments def myfunction(first, second, third=None): # third is optional 

however, if you really want to do this, you can certainly make it work (at the risk of offending the traditionalists; o). In short, you should write a wrapper(*args) function that checks the number of arguments and delegates as necessary. such a β€œhack” is usually done through decorators. in this case, you could achieve something like:

 from typing import overload @overload def myfunction(first): .... @myfunction.overload def myfunction(first, second): .... @myfunction.overload def myfunction(first, second, third): .... 

and you implement this if the overload(first_fn) (or constructor) function returns the called object, where the __call__(*args) method contains the explanation described above, and the overload(another_fn) method adds additional functions that can be delegated.

you can see an example of something similar here http://acooke.org/pytyp/pytyp.spec.dispatch.html , but these are overload methods by type. this is a very similar approach ...

UPDATE: and something similar (using argument types) is added in python 3 - http://www.python.org/dev/peps/pep-0443/

+34
Aug 18 '11 at 19:40
source share

Yes it is possible. I wrote the code below in Python 3.2.1:

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

Using:

 myfunction=overload(no_arg_func, one_arg_func, two_arg_func) 

Note that the lambda returned by the overload functions selects the function to call depending on the number of arguments without a name .

The solution is not perfect, but at the moment I can not write anything better.

+5
Oct 24 '14 at 19:44
source share

Impossible directly. You can use explicit type checks on the arguments given, although this is generally disapproving.

Python is dynamic. If you don’t know what the object can do, just try: and call the method on it, except for: errors.

If you don't need to overload based on types, but simply by the number of arguments, use keyword arguments.

+1
Aug 18 '11 at 19:33
source share

overload methods are complicated in python. However, there may be use of passing a dict, list, or primitive variables.

I tried something for my use cases, this can help people understand to overload methods.

Let's look at an example in one of the stackoverflow threads:

class overload method with calling methods from another class.

def add_bullet(sprite=None, start=None, headto=None, spead=None, acceleration=None):

pass arguments from a remote class:

add_bullet(sprite = 'test', start=Yes,headto={'lat':10.6666,'long':10.6666},accelaration=10.6}

OR add_bullet(sprite = 'test', start=Yes,headto={'lat':10.6666,'long':10.6666},speed=['10','20,'30']}

So, the processing is performed for a list, a dictionary, or primitive variables from a method overload.

try for your codes

0
Dec 30 '16 at 6:44
source share



All Articles