I have a fancyfunction
defined to do something to a single argument. I decorate it to become a generic function so that it knows what to do if it is given a tuple.
from functools import singledispatch
@singledispatch
def fancyfunction(arg):
print(arg)
@fancyfunction.register
def _(arg: tuple):
for a in arg:
fancyfunction(a)
Sure enough the valid call signatures for the two functions above are:
fancyfunction(foo)
fancyfunction((foo, bar, ...))
What I want to do
I want to simplify the call signatures so that I don't need the extra pair of parentheses:
fancyfunction(foo)
fancyfunction(foo, bar, baz, ...)
In order to do that, I need the overloaded function to unpack its positional arguments:
@singledispatch
def fancyfunction(arg):
print(arg)
@fancyfunction.register
def _(*arg):
for a in arg:
fancyfunction(a)
Of course the last code snippet above doesn't work. Doing this:
fancyfunction(foo, bar, baz)
would call the generic function instead of the overloaded function.
Is it possible to make singledispatch
recognize that the decorated function is called with *
-form of arguments? P.S. What is the official name of that kind of call signature?