I would like to use overloading in Python. I know it's not possible by design (Python is dynamically typed language), there is quite good thread here on this topic. That's why I could use something like multiple dispatch (multimethods). And I'm thinking about the best solution, that would implement multiple dispatch for any function or class method. Is there any solution that is natively included in Python?
Using singledispatch (added to Python in 3.4) is not enough in my case (because only the type of the first argument is considered and I need multiple).
I also know that I could use keyword arguments (i.e. having single function and drive the behavior by checking all arguments), but it's getting complicated when you have a lot of arguments and combinations.
So far, I was able to find these two libraries (not included in the Python Standard Library):
multimeta
namespace for classesThe basic usage looks like:
from multimethod import multimethod
@multimethod
def add(x: int, y: int):
...
@multimethod
def add(x: str, y: str):
...
or
from multipledispatch import dispatch
@dispatch(int, int)
def add(x, y):
...
@dispatch(str, str)
def add(x, y):
...
This functionality seems very similar.
I have mainly two questions:
My reasons on why multimethod is vastly better compared to multipledispatch