Package: overload-function
# Overload FunctionThis allows python to overload functions like C++. It uses function argument type annotations to do this.## Example - Simple```pythonimport overload_functionclass Test(object):def __init__(self, x=0):self.x = xself.int_test = Noneself.bool_test = None@overload_functiondef set_x(self, x: int):self.int_test = Trueself.x = x@set_x.overloaddef set_x(self, x: bool):self.bool_test = Trueself.x = 0t = Test()assert t.x == 0value = 2t.set_x(value)assert t.x == valueassert t.int_testvalue = Falset.set_x(value)assert t.x == 0assert t.bool_test```## Example - advanced```pythonfrom overload_function import overload_functionclass Point(object):def __init__(self, x=0, y=0):self.x = xself.y = ydef __eq__(self, obj):try:return self.x == obj.x and self.y == obj.yexcept:return Falseclass Test(object):@overload_functiondef __init__(self, s: str="", x: int=0, b: bool=False, p: Point=Point()):self.s = sself.x = xself.b = bself.p = p@__init__.overloaddef __init__(self, x: int=0, b: bool=False, s: str="", p: Point=Point()):self.s = sself.x = xself.b = bself.p = p@__init__.overloaddef __init__(self, p: Point=Point(), x: int=0, b: bool=False, s: str=""):self.s = sself.x = xself.b = bself.p = pt1 = Test("Hello World!", 1, True, Point(1, 1))assert t1.s == "Hello World!"assert t1.x == 1assert t1.b is Trueassert t1.p == Point(1, 1)# Overload x firstt2 = Test(1, True, "Hello World!", Point(1, 1))assert t2.s == "Hello World!"assert t2.x == 1assert t2.b is Trueassert t2.p == Point(1, 1)# Overload p firstt3 = Test(Point(1, 1), 1, True, "Hello World!")assert t3.s == "Hello World!"assert t3.x == 1assert t3.b is Trueassert t3.p == Point(1, 1)```## Example - function```pythonimport overload_functiont1 = []t2 = []@overload_functiondef run1(x: int=0, y: int=0):t1.append((x, y))@run1.overloaddef run1(x: str="1", y: str="1"):t2.append((int(x), int(y)))run1(2, 2)assert t1 == [(2, 2)]run1("3", "3")assert t2 == [(3, 3)]```## Example - custom match function```pythonimport overload_functiondef match_on_overloader(given_args, given_kwargs, func, spec_arg_names, spec_annotations, spec_defaults):"""Compare arguments and return a weight for how much you want to use this function.Args:given_args (tuple): Arguments given to the function call.given_kwargs (dict): Keyword arguments given to the function call.func (function): Function that you are checking arguments forspec_arg_names (list): List of argument names in order found with inspect.getfullargspec.spec_annotations (dict): Dictionary of spec argument names and type annotations to try and match.spec_defaults (tuple): Default values that match the spec_arg_names.Returns:weight (int): Integer that dictates how likely this function is to be the correct function. The highestnumber will be used in deciding which function should be called."""try:idx = spec_arg_names.index("overloader")overloader_val = spec_defaults[idx]if overloader_val == given_kwargs["overloader"]:return float("inf")except:passreturn 0# return overload_function.match(given_args, given_kwargs,#                                func, spec_arg_names, spec_annotations, spec_defaults)class Test(object):@overload_function(match_func=match_on_overloader)def __init__(self, s:str="", x:int=0, b:bool=False, overloader:None="first"):self.s = sself.x = xself.b = bprint("overloader", overloader)@__init__.overloaddef __init__(self, x:int=0, b:bool=False, s:str="", overloader:None="second"):self.s = sself.x = xself.b = bprint("overloader", overloader)@__init__.overloaddef __init__(self,  b:bool=False, x:int=0, s:str="", overloader:None="third"):self.s = sself.x = xself.b = bprint("overloader", overloader)t = Test(1, 2, 3, overloader="third")assert t.s == 3assert t.x == 2assert t.b == 1t = Test(1, 2, 3, overloader="first")assert t.s == 1assert t.x == 2assert t.b == 3t = Test(1, 2, 3, overloader="second")assert t.s == 3assert t.x == 1assert t.b == 2t = Test(1, 2, 3)  # Will used the first function set (overloader=="first")assert t.s == 1assert t.x == 2assert t.b == 3```