from multimethod import multimethod
@multimethod
def func(*x: int):
print('int')
@multimethod
def func(*x: str):
print('str')
func('')
func(1)
預期output:
str
int
實際output:
str
str
是不是沒有任何一個overload的包可以實現任意數量參數的函式?
from multimethod import multimethod
@multimethod
def func(*x: int):
print('int')
@multimethod
def func(*x: str):
print('str')
func('')
func(1)
預期output:
str
int
實際output:
str
str
是不是沒有任何一個overload的包可以實現任意數量參數的函式?
從 Usage — multimethod 1.4 documentation
Dispatch resolution details:
- Additional
*args
or**kwargs
may be used when calling, but won’t affect the dispatching.
我不確定這裡是指額外沒指定的,還是概指所有 *
, **
但有其他方法可以繞過
@multimethod
def func(x: tuple[int, ...]):
print('int')
@multimethod
def func(x: tuple[str, ...]):
print('str')
這樣的話
只要給入 tuple
就會可以辨別
func((1, )) # (1) -> 還是只傳 1 而非 tuple
func(('1', ))
output
int
str
那如果想要直接達到原本使用 *x
的話就可以加上額外一個 function
def func_alt(*x):
func(x) # x is tuple
這樣
func_alt(1)
func_alt('1')
就也會是
int
str