python isinstance 判断各种类型的小细节
1. 基本语法
isinstance(object, classinfo)
Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. Also return true if classinfo is a type object (new-style class) and object is an object of that type or of a (direct, indirect or virtual) subclass thereof. If object is not a class instance or an object of the given type, the function always returns false. If classinfo is neither a class object nor a type object, it may be a tuple of class or type objects, or may recursively contain other such tuples (other sequence types are not accepted). If classinfo is not a class, type, or tuple of classes, types, and such tuples, a TypeError exception is raised.
classinfo 处可以是 a class, type, or tuple of classes and types,
如果是 tuple,则满足 tuple 中的任何一个即返回 True
2. 字符串的类型判断。
字符串,分为 str 和 unicode,二者均继承自 basestring
>>> isinstance(u'3.0', unicode)
True
>>> isinstance('3.0', str)
True
>>> isinstance(u'3.0', str)
False
>>> isinstance(u'3.0', str)
False
>>> isinstance(u'3.0', basestring)
True
>>> isinstance('3.0', basestring)
True
3. 数字的类型判断
数字分为 int 和 float,暂未发现二者共同的有效父类。
可以用 (int, float) tuple 来判断是否为数字(int 或 float)
>>> isinstance('', (int, float))
False
>>> isinstance(3.0, (int, float))
True
>>> isinstance(3, (int, float))
True
>>> isinstance(3.0, float)
True
>>> isinstance(3.0, int)
False
>>> isinstance(3, float)
False
>>> isinstance(3, int)
True
python isinstance 判断各种类型的小细节的更多相关文章
- Python isinstance判断对象类型
在Python中只需要使用内置的函数isinstance,使用起来非常简单,比如下面的例子: class objA: pass A = objA() B = 'a','v' C = 'a string ...
- Python中请使用isinstance()判断变量类型
一.isinstance() 在Python中可以使用type()与isinstance()这两个函数判断对象类型,而isinstance()函数的使用上比type更加方便. # coding=utf ...
- python准确判断文件类型
判断文件类型在开发中非常常见的需求,怎样才能准确的判断文件类型呢?首先大家想到的是文件的后缀,但是非常遗憾的是这种方法是非常不靠谱的,因为文件的后缀是可以随意更改的,而大家都知道后缀在linux系统下 ...
- python中判断对象类型的函数——isinstance
isinstance是Python中的一个内建函数.是用来判断一个对象的变量类型. isinstance(object, class-or-type-or-tuple) 如果参数object是clas ...
- 【python】【转】python中isinstance判断变量类型用法
来源 http://www.jb51.net/article/15696.htm 在Python中只需要使用内置的函数isinstance,使用起来非常简单,比如下面的例子: 复制代码 代码如下: c ...
- python实例[判断操作系统类型]
参考文献:http://bbs.chinaunix.net/thread-1848086-1-1.html 经常地我们需要编写跨平台的脚本,但是由于不同的平台的差异性,我们不得不获得当前所工作的平台( ...
- Python正则表达式使用过程中的小细节
今天用Python写了个简单的爬虫程序,抓取虎扑篮球(nba.hupu.com)的首页内容,代码如下: #coding:gb2312 import urllib2, re webpage = urll ...
- python isinstance()判断数据类型
举例: d = (1,2,3) print(isinstance(d,int)) #False print(isinstance(d,tuple)) #True print(isinstance(d, ...
- Python中为什么推荐使用isinstance来进行类型判断?而不是type
转自:http://www.xinxingzhao.com/blog/2016/05/23/python-type-vs-isinstance.html Python在定义变量的时候不用指明具体的的类 ...
随机推荐
- cocos2d-x Sprite
转自:http://codingnow.cn/cocos2d-x/795.html 精灵是游戏中十分重要的组成部分,随处可见,如:游戏背景.NPC.人物.道具等.在cocos2d-x引擎中,只要是用图 ...
- mac os升级为 Yosemite 10.10 后不能创建javaproject
出现这样的情况可能是因为mac系统升级导致JAVA的安装路径发生改变(Xcode升级后也出现模拟器位置改变的情况,不要奇怪) 之前安装的eclipse就自然找不到SDK的路径了,所以会捆绑失败 接下来 ...
- [Webpack] Use the Webpack Dashboard to Monitor Webpack Operations
Learn how to use the new Webpack Dashboard from Formidable Labs to display a pretty, useful output f ...
- android学习日记03--常用控件Dialog
常用控件 9.Dialog 我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框 对话框,要创建对话框之前首先要创建Bui ...
- nginx学习七 高级数据结构之动态数组ngx_array_t
1 ngx_array_t结构 ngx_array_t是nginx内部使用的数组结构.nginx的数组结构在存储上与大家认知的C语言内置的数组有相似性.比方实际上存储数据的区域也是一大块连续的内存. ...
- IE jquery mouseenter,mouseover超奇葩问题
做了个项目,结构很简单 <div class="index-main" data-url="./img/index_default.jpg"> &l ...
- eclipse git插件配置
一.Eclipse上安装GIT插件EGit Eclipse的版本eclipse-java-helios-SR2-win32.zip(在Eclipse3.3版本找不到对应的 EGit插件,无法安装) E ...
- VB.NET中使用代表对方法异步调用
按照我们常规的思维方式,计算机应该是干完一件事,然后再干下一件.用术语来说,这种执行任务的方式叫做同步执行(Synchronous Execution).既然这样,那么为什么要引入异步执行的概念呢? ...
- 如何让PHP支持Redis
原理:php默认扩展库不含有redis扩展:要支持redis扩展,需要有redis.so这个扩展文件 所以我们的目标就是生成redis.so扩展文件,并修改php.ini 让其支持redis扩展. 准 ...
- Java项目打包在CMD或者Linux下运行
Java项目打包在CMD或者Linux下运行 1.在CMD下运行 在eclipse中将项目export成jar包,然后用压缩软件解压