类型判断
from functools import singledispatch
import numbers
from collections import abc
from collections import Iterable
@singledispatch
def console(obj):
print("未知类型", obj)
@console.register(str)
def _(text):
print("文本", text)
@console.register(numbers.Integral)
def _(n):
print("数字", n)
@console.register(tuple)
@console.register(abc.MutableSequence)
def _(seq):
print("这是一个seq", seq)
@console.register(dict)
def _(dic):
print("这是一个字典", dic)
@console.register(Iterable)
def _(iters):
print("这是一个可迭代类型",iters)
if __name__ == '__main__':
console(1)
console('a')
console([1, 2, 3, 4, 5, 6])
console(range(8))