Python 代码优化基础——判断对象类型
2015-06-20 19:49
981 查看
# -*- coding: utf-8 -*- # # def displayNumType(num): # print num, 'is', # if type(num) == type(0): # print 'an integer' # elif type(num) == type(0L): # print 'a long' # elif type(num) == type(0.0): # print 'a float' # elif type(num) == type(0+0j): # print 'a complex number' # else: # print 'not a number at all!!' # # # #减少函数调用的次数 # #代码在进行判断时使用了两次type()函数,我们使用types模块中的变量代替之 # import types # # if type(num) == types.IntType: # pass # # #对象身份比较优于对象值比较 # #值比较: # if type(num) == type(0): # pass # #对象身份比较: # if type(num) is types.IntType: # pass # # #减少查询次数 # #import types # from types import IntType # if type(num) is IntType: # pass # # #惯例风格可读性的考虑:使用isinstance() # # # 最终代码 def displayNumType(num): print num ,'is', #isinstance同时判断多个种类的用法 #如果是这四个其中之一 if isinstance(num,(int, long, float, complex)): #返回这个type的名字 print 'a number of type:', type(num).__name__ else: print 'not a number at all!!' displayNumType(-69) displayNumType(9999999999999999999999999L) displayNumType(98.6) displayNumType(-5.2+1.9j) displayNumType('xxx')
相关文章推荐
- python下requests模块的安装方法
- python安装lxml,在windows环境下
- Python性能(转)
- 【练习题】读取一个字符串,计算每个字母出现的个数
- python 获取网页
- IPython的基本功能(转)
- Python安装第三方库的三种方法
- Python Show-Me-the-Code 第 0023 题 Web留言簿应用
- Python strange questions list
- python正则表达式
- BlenderPython (三)bpy模块
- python 输出中文
- Python包管理工具——Pip
- python pip通过本地目录安装相关程序包
- python的pip和virtualenv使用心得
- Python----matplotlib详细介绍
- Python2.7 中安装 matplotlib
- Ubuntu14.04下配置Emacs的Python IDE环境
- 如何在Windows环境下运行Python脚本
- python在控制台输出进度条的方法