1

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>

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Gregory
  • 33
  • 4

1 Answers1

0

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
  • this throws an error... the same one the OP mentions. – Alexander Jun 02 '22 at 16:07
  • Please retry. I tested and it worked fine on python 3.7.3. – Nguyễn Minh Hiếu Jun 02 '22 at 16:11
  • So your suggestion is that he add unnecessary keyword arguments which the documentation specifically states it doesn't support. All so in the end the keyword arguments are useless anyway because the OP is forced to include them in the function signature – Alexander Jun 02 '22 at 16:12
  • The dispatch does support keyword arguments. It's required if you want to use default parameters (see this post: https://stackoverflow.com/questions/54132640/how-to-use-default-parameter-with-multipledispatch). I'm trying to help OP accomplish what he/she asked, because we maybe do not know the meaning of the keyword argument with default value without context. – Nguyễn Minh Hiếu Jun 02 '22 at 16:14
  • I am still getting an error with all of the function calls you listed – Alexander Jun 02 '22 at 16:17
  • Weird. May I ask what version of python are you using so I can test on that version? – Nguyễn Minh Hiếu Jun 02 '22 at 16:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245272/discussion-between-nguyn-minh-hiu-and-alexpdev). – Nguyễn Minh Hiếu Jun 02 '22 at 16:21