使用type()
首先,我们来判断对象类型,使用 type() 函数:
基本类型都可以用 type() 判断:
>>> type(123)
<class 'int'>
>>> type('str')
<class 'str'>
>>> type(None)
<type(None) 'NoneType'>
如果一个变量指向函数或者类,也可以用 type() 判断:
>>> type(abs)
<class 'builtinfunctionor_method'>
>>> type(a)
<class '__main.Animal'>
但是 type() 函数返回的是什么类型呢?它返回对应的Class类型。如果我们要在 if 语句中判断,就需要比较两个变量的type类型是否相同:
>>> type(123)==type(456)
True
>>> type(123)==int
True
>>> type('abc')==type('123')
True
>>> type('abc')==str
True
>>> type('abc')==type(123)
False
判断基本数据类型可以直接写 int , str 等,但如果要判断一个对象是否是函数怎么办?可以使用 types 模块中定义的常量:
>>> import types
>>> def fn():
… pass
…
>>> type(fn)==types.FunctionType
True
>>> type(abs)==types.BuiltinFunctionType
True
>>> type(lambda x: x)==types.LambdaType
True
>>> type((x for x in range(10)))==types.GeneratorType
True