""" Multiple Dispatch is *the* feature in Julia that makes it great. So there's a port to Python as well, but it had to be programmed since it's not in the heart of Python. See https://en.wikipedia.org/wiki/Multiple_dispatch https://pypi.org/project/multipledispatch/ https://pypi.org/project/multimethod/ """ from multipledispatch import dispatch from multimethod import multimethod # The point of multiple dispatch is to avoid isinstance # the same functionality in plain Python def multiadd(x, y): if isinstance(x, int) and isinstance(y, int): return x + y elif isinstance(x, object) and isinstance(y, object): return "%s + %s" % (x, y) else: raise TypeError("unsupported argument types (%s, %s)" % (type(x), type(y))) res1 = multiadd(1, 2) res2 = multiadd(1, 'hello') print('res1: ',res1) print('res2: ',res2) # 1st solution: multipledispatch @dispatch(int, int) def add(x, y): return x + y @dispatch(object, object) def add(x, y): return "%s + %s" % (x, y) res3 = add(1, 2) res4 = add(1, 'hello') print('res3: ',res3) print('res4: ',res4) # 2nd solution: multimethod # pros: type hints in function calls, not in the decorator # fastest implementation one can have in Python ? @multimethod def add(x :int, y :int): return x + y @multimethod def add(x :object, y :object): return "%s + %s" % (x, y) res5 = add(1, 2) res6 = add(1, 'hello') print('res5: ',res5) print('res6: ',res6) """ If you have just def add(x, y): return x + y then add(1, 'hello') gives TypeError: unsupported operand type(s) for +: 'int' and 'str' """