Home > Mobile >  Multipledispatch in python
Multipledispatch in python

Time:06-03

Can anyone explain why it doesn't work? I would like to have the same second argument for each function.

from multipledispatch import dispatch

@dispatch(str, ss=list)
def Func(s, ss=[]):
    return s

@dispatch(list, ss=list)
def Func(l, ss=[]):
    return Func(l[0], ss)

Func(["string"])

The error is: Could not find signature for Func: <str, list>

CodePudding user response:

Because you use keyword arguments in @dispatch, you must use keyword arguments when calling the function as it already used keyword ss as signature to be discoverable.

@dispatch(str, ss=list)
def Func(s, ss=[]):
    return s


@dispatch(list, ss=list)
def Func(l, ss=[]):
    return Func(l[0], ss=ss)

Func(["string"])  # output: string
Func("string", [])  # calling this will raise error
Func("string", ss=[])  # output: string
  • Related