type函数可以检测任何值或变量的类型。
例子:
def printType(var):
print(type(var)) class TestClass:
pass printType(1)
printType(1.5)
printType('str')
printType(True)
printType(['a','b','c'])
printType(('a','b','c'))
printType(set(['a','b','c']))
printType({'Tom':18,'Lily':20})
printType(printType)
printType(TestClass)
printType(TestClass())
将会依次输出:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'list'>
<class 'tuple'>
<class 'set'>
<class 'dict'>
<class 'function'>
<class 'type'>
<class '__main__.TestClass'>
type函数相当于C#中的typeof和GetType的混合体