
看下面的例子,应该知道怎么去使用它完成函数的重载。
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])结果为:
1 <class 'int'> int xx <class 'str'> str [1] <class 'list'> obj












