1. 基本语法

isinstance(objectclassinfo)

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

  1. >>> isinstance(u'3.0', unicode)
  2. True
  3. >>> isinstance('3.0', str)
  4. True
  5. >>> isinstance(u'3.0', str)
  6. False
  7. >>> isinstance(u'3.0', str)
  8. False
  9. >>> isinstance(u'3.0', basestring)
  10. True
  11. >>> isinstance('3.0', basestring)
  12. True

3. 数字的类型判断

数字分为 int 和 float,暂未发现二者共同的有效父类。

可以用 (int, float) tuple 来判断是否为数字(int 或 float)

  1. >>> isinstance('', (int, float))
  2. False
  3. >>> isinstance(3.0, (int, float))
  4. True
  5. >>> isinstance(3, (int, float))
  6. True
  7. >>> isinstance(3.0, float)
  8. True
  9. >>> isinstance(3.0, int)
  10. False
  11. >>> isinstance(3, float)
  12. False
  13. >>> isinstance(3, int)
  14. True

python isinstance 判断各种类型的小细节的更多相关文章

  1. Python isinstance判断对象类型

    在Python中只需要使用内置的函数isinstance,使用起来非常简单,比如下面的例子: class objA: pass A = objA() B = 'a','v' C = 'a string ...

  2. Python中请使用isinstance()判断变量类型

    一.isinstance() 在Python中可以使用type()与isinstance()这两个函数判断对象类型,而isinstance()函数的使用上比type更加方便. # coding=utf ...

  3. python准确判断文件类型

    判断文件类型在开发中非常常见的需求,怎样才能准确的判断文件类型呢?首先大家想到的是文件的后缀,但是非常遗憾的是这种方法是非常不靠谱的,因为文件的后缀是可以随意更改的,而大家都知道后缀在linux系统下 ...

  4. python中判断对象类型的函数——isinstance

    isinstance是Python中的一个内建函数.是用来判断一个对象的变量类型. isinstance(object, class-or-type-or-tuple) 如果参数object是clas ...

  5. 【python】【转】python中isinstance判断变量类型用法

    来源 http://www.jb51.net/article/15696.htm 在Python中只需要使用内置的函数isinstance,使用起来非常简单,比如下面的例子: 复制代码 代码如下: c ...

  6. python实例[判断操作系统类型]

    参考文献:http://bbs.chinaunix.net/thread-1848086-1-1.html 经常地我们需要编写跨平台的脚本,但是由于不同的平台的差异性,我们不得不获得当前所工作的平台( ...

  7. Python正则表达式使用过程中的小细节

    今天用Python写了个简单的爬虫程序,抓取虎扑篮球(nba.hupu.com)的首页内容,代码如下: #coding:gb2312 import urllib2, re webpage = urll ...

  8. python isinstance()判断数据类型

    举例: d = (1,2,3) print(isinstance(d,int)) #False print(isinstance(d,tuple)) #True print(isinstance(d, ...

  9. Python中为什么推荐使用isinstance来进行类型判断?而不是type

    转自:http://www.xinxingzhao.com/blog/2016/05/23/python-type-vs-isinstance.html Python在定义变量的时候不用指明具体的的类 ...

随机推荐

  1. iOS新特性引导页

    有一个注意点: 获取版本号 个叫做Version,一个叫做Build,这两个值都可以在Xcode 中选中target,点击"Summary"后看到. Version在plist文件 ...

  2. 【转】于request.getSession(true/false/null)的区别

    http://blog.csdn.net/gaolinwu/article/details/7285783 关于request.getSession(true/false/null)的区别 一.需求原 ...

  3. hdu 5442 Favorite Donut 后缀数组

    Favorite Donut Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...

  4. joa-framework 工作流高速开发框架(jeecg官方工作流版本号) 公布

    -------------------------------------- version:  joa-framework1.0.0.beta 版本号: JOA 工作流高速开发框架   Date:  ...

  5. 设计模式 - 命令模式(command pattern) 多命令 具体解释

    命令模式(command pattern) 多命令 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.csdn.ne ...

  6. javascript关闭浏览器窗口

    var opened=window.open('about:blank','_self'); opened.close(); window.open 不被阻止 .open('_blank') 然后 t ...

  7. 学习笔记之Shell & QSHELL

    shell(计算机壳层)_百度百科 http://baike.baidu.com/subview/849/15831672.htm Shell (computing) - Wikipedia, the ...

  8. mysql服务器辅助选项

    查看控制台命令行前缀 : echo $PS1 ,例如输出 '[\u@\h \w]#     其中,\u是用户名,\h是主机名称: hostname -s 可以查看当前主机名,  hostname 'z ...

  9. (一)javascript中的数组index属性——获取数组的索引值

    例如:要做到这样的效果 点击每个选项时,会显示不同的div. 我们的做法:在javascript中,先把所有的div的display设置为none,然后在根据当前的数组里的索引值进行一个显示div的过 ...

  10. React Editor 应用编辑器(1) - 拖拽功能剖析

    这是可视化编辑器 Gaea-Editor 的第一篇连载分析文章,希望我能在有限的篇幅讲清楚制作这个网页编辑器的动机,以及可能带来的美好使用前景(画大饼).它会具有如下几个特征: 运行在网页 文档流布局 ...