您的位置:首页 > 其它

使用isinstance()来判断一个对象的类型

2015-08-13 17:28 253 查看
#!/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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: