使用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)
# 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)
相关文章推荐
- postgresql 查数据库总大小(占用磁盘空间)
- NOIP2012 同余方程 题解
- SOAPUI 断言设置之二
- hdu 1316 How Many Fibs?
- NB多项式模型、神经网络、SVM初步—斯坦福ML公开课笔记6
- CSS3选择器学习笔记
- ubuntu安装ulipad
- 邓小平文选第三卷辅导讲座
- 调用百度语音SDK,简单的语音识别控件
- 关于无线PCB中 中50欧姆的特性阻抗的注意事项
- android 图片上传,使用 httpclient 的MultipartEntity 上传
- CMD命令不能运行
- Linux Ubuntu 环境变量修改及生效方法
- IOS BLE4.0蓝牙和外设连接和收发数据的流程
- uC/OS-II中OSUnMapTbl[]的原理
- asp.net Cache vs memcached
- 【NOI2015Day1】【bzoj4195】【bzoj4196】【bzoj4197】
- 【dp】【NOI 2015】【bzoj 4197】寿司晚宴
- MFC 单文档去掉标题栏和菜单栏
- 解决模拟弹出框情况下文档滚动的问题