python中functools.singledispatch的使用

时间:2023-03-08 23:00:32
python中functools.singledispatch的使用
from functools import singledispatch

@singledispatch
def show(obj):
print (obj, type(obj), "obj") @show.register(str)
def _(text):
print (text, type(text), "str") @show.register(int)
def _(n):
print (n, type(n), "int")
show(1)
show("xx")
show([1])

python中functools.singledispatch的使用

为show函数传递不同的类型参数,就表现不同的行为