使用isinstance()来判断一个对象的类型
#!/usr/bin/env python
# coding: utf-8
'ct_p14.py -- use isinstance() to tell the type of a value' #此处为脚本doc文档
#print("Enter something, you will see its type.") #本想用用户输入的方式来获取一个对象,可是输入的对象进行判断都是字符串,强制转换如果是字符串又会报错
#define the function
def displayNumType(num):
print(num),
if isinstance(num, (int, long, float, str, complex)): #使用isinstance()函数
print("is type of %s." %type(num).__name__) #type(num).__name__ 获取num对象的type类型
else:
print("%s is a type unknow.")
displayNumType(-69)
displayNumType('55')
displayNumType(555.7)
displayNumType('dd')
displayNumType('fg2369')
displayNumType(1.6j)
displayNumType(999999999999999999999999999999999L)