How to Add Function Overloads to Python

How to Add Function Overloads to Python

Python Programming Assignment HelpPython does not allow you to overload a function based on the type of arguments, well, until now that is. This decorator allows you to call a method depending on the type of the arguments with a clean syntax using Python 3.6+ type annotations, this is only designed for use with standalone functions and would need some changes before adapting it to work on class methods.

from functools import wraps

__overloads = {}

def overload(func):

@wraps(func)

def wrap(*args, **kwargs):

if name not in __overloads:

__overloads[name] = {}

overloads = __overloads[name]

call = overloads.get(tuple(type(arg) for arg in args))

if call:

return call(*args, **kwargs)

else:

print(“Unknown argument type found for”, name)

#raise Exception(“Unknown argument type found for”, name)

name = func.__name__

annotations = tuple(arg for arg in func.__annotations__.values())

if name not in __overloads:

__overloads[name] = {}

overloads = __overloads[name]

overloads[annotations] = func

return wrap

@overload

def test(val : int):

print(“Int”, val)

@overload

def test(val : str):

print(“Str”, val)

@overload

def test(val : list):

print(“List”, val)

def main():

test(3)

test(“3”)

test([1, 2, 3])

test(1.0)

if __name__ == “__main__”:

main()

When you run this code you will get output like the following:

Int 3

Str 3

List [1, 2, 3]

Unknown argument type found for test

This is similar to the code works in C++, and dispatches based on the type of arguments so it requires Python 3.6 or greater since it relies on function annotations. This is an incomplete implementation, you could add the ability to say val : (int, float) and it would store 2 dictionary entries so either could be called, or you could do a fallback when it doesn’t find a matching signature, that it checks using isinstance and then adds the entry to the dictionary if it finds a match using that. There is a decorator called singledispatch in the function tools package that is similar, but I think this has a nicer syntax and it also uses all of the types passed (it doesn’t check keyword arguments, although you can still use them). If you want to use it inside a class, you will need to make some changes, which we can do for you if you want to use our Python programming assignment help for you own project.

Decorators are a powerful feature of Python that allow you to run code each time the method is run, you can use a simple decorator to provide a logging feature, or for profiling. The wraps call allows to preserve the doc string if one is present, I commented out the exception and just printed but you should probably change this if you want to use it in production, it was just easier to demonstrate.

Programming Homework Helper are able to provide Python programming homework help with any version of Python (2, 3, Jython, Ipython notebbok) on Mac, Windows or Linux. We don’t just work on Python but on a variety of programming languages including Java, C/C++, Matlab, SQL and assembly language, as well as web pages using Javascript or any of it’s libraries.