Open ToniBig opened 2 years ago
Isn't that what functools.singledispatch
does? https://docs.python.org/3/library/functools.html
Using that seems kind of clunky, since you have to use @fun.register
also. As stated above I was thinking of a interface similar to multidispatch without to need to redundantly provide types to the decorator.
I think it is auto inferred now:
from multipledispatch import dispatch
if __name__ == "__main__":
@dispatch()
def add(x: int, y: int):
return x + y
@dispatch()
def add(x: object, y: object):
return "%s + %s" % (x, y)
print(add(1, 2)) # 3
print(add("1", "2")) # 1+2
It doesn't work when there are kwargs unless explicity stated:
from multipledispatch import dispatch
@dispatch()
def add(x: int, y: int):
return x + y
@dispatch(object,object)
def add(x: object, y: object,jesus=None):
return "%s + %s" % (x, y)
print(add(1, 2)) # 3
print(add("1", "2")) # 1+2
Works
from multipledispatch import dispatch
@dispatch()
def add(x: int, y: int):
return x + y
@dispatch()
def add(x: object, y: object,jesus=None):
return "%s + %s" % (x, y)
print(add(1, 2)) # 3
print(add("1", "2")) # 1+2
Doesn't work
It would be great to have a decorator that would not need an explicit parameter signature , when all of a functions parameters were annotated using typing capabilities. If this possibily already exists please tell me how it's done.
Right now I have to / can use the dispactch decorator like this:
In the case of fully annotated functions would it not be possibly to provide a decorator that would work just like that(?):