Python 判断数据类型有type和isinstance

基本区别在于:

type():不会认为子类是父类

isinstance():会认为子类是父类类型

class Color(object):
pass

class Red(Color):
pass

print type(Color()) == Color
print type(Red()) == Color
print isinstance(Red(),Color)
执行结果如下:

True
False
True

用isinstance判断mongDB中的一些数据类型:

字符串、int、long、float - isinstance(data, (int, str, types.LongType, float))
时间类型 - isinstance(data, datetime.datetime)
布尔类型 - isinstance(data, (bool))
字典类型 - isinstance(data, (dict))
数组 - isinstance(data, (list))
unicode - isinstance(data, unicode)
mongo obJect - isinstance(data, bson.objectid.ObjectId)

可以引入types模板,获取数据类型:

inport types

types取值:

  BooleanType
  BufferType
  BuiltinFunctionType
  BuiltinMethodType
  ClassType
  CodeType
  ComplexType
  DictProxyType
  DictType
  DictionaryType
  EllipsisType
  FileType
  FloatType
  FrameType
  FunctionType
  GeneratorType
  GetSetDescriptorType
  InstanceType
  IntType
  LambdaType
  ListType
  LongType
  MemberDescriptorType
  MethodType
  ModuleType
  NoneType
  NotImplementedType
  ObjectType
  SliceType
  StringType
  StringTypes
  TracebackType
  TupleType
  TypeType
  UnboundMethodType
  UnicodeType
  XRangeType

python 2 实例:

import types
type(x) is types.IntType # 判断是否int 类型
type(x) is types.StringType #是否string类型

还可以:

import types
type(x) == types(1) # 判断是否int 类型
type(x) == type('a') #是否string类型

python 3 实例:

if type(fileJson) is dict:
if type(fileJson) == dict:
if type(fileJson) == type({}):

python 判断数据类型及释疑的更多相关文章

  1. (转)python 判断数据类型

    原文:https://blog.csdn.net/mydriverc2/article/details/78687269 Python 判断数据类型有type和isinstance 基本区别在于: t ...

  2. Python输入数据类型判断正确与否的函数大全(非常全)

      对于python输入数据类型判断正确与否的函数大致有三类: (1)type(),它的作用直接可以判断出数据的类型 (2)isinstance(),它可以判断任何一个数据与相应的数据类型是否一致,比 ...

  3. python 基本数据类型分析

    在python中,一切都是对象!对象由类创建而来,对象所拥有的功能都来自于类.在本节中,我们了解一下python基本数据类型对象具有哪些功能,我们平常是怎么使用的. 对于python,一切事物都是对象 ...

  4. python常用数据类型内置方法介绍

    熟练掌握python常用数据类型内置方法是每个初学者必须具备的内功. 下面介绍了python常用的集中数据类型及其方法,点开源代码,其中对主要方法都进行了中文注释. 一.整型 a = 100 a.xx ...

  5. python之数据类型详解

    python之数据类型详解 二.列表list  (可以存储多个值)(列表内数字不需要加引号) sort s1=[','!'] # s1.sort() # print(s1) -->['!', ' ...

  6. python基本数据类型之集合

    python基本数据类型之集合 集合是一种容器,用来存放不同元素. 集合有3大特点: 集合的元素必须是不可变类型(字符串.数字.元组): 集合中的元素不能重复: 集合是无序的. 在集合中直接存入lis ...

  7. python基本数据类型之字符串(四)

    python基本数据类型之字符串(四) 判断方法 python中有一类用来判断字符串形式的方法,该类方法有两个特点:(1)方法名都是is开头(除了startswith和endswith):(2)返回值 ...

  8. python基本数据类型之字符串(三)

    python基本数据类型之字符串(三) 转换和判断方法 在python中,有一些内置方法可以将字符串转化特定形式,而与之对应的一些方法可以判断字符串是否符合某些形式.因此,在这篇文章中,笔者把转换方法 ...

  9. Python的数据类型和运算

    一.Python数据类型 变量所指向的值是有自己独特的数据类型的,这些数据类型可能代表不同的数据,在Python中,主要有以下几种数据类型: 整形(int) 在计算机中,整数的位数其实是有范围的,并没 ...

随机推荐

  1. checkBox复选框,获得选中那一行所有列的数据

    function showCol(){ var check=$("input[name='one']:checked");//选中的复选框 check.each(function( ...

  2. HanLP封装为web services服务的过程介绍

    前几天的召开的2019年大数据生态产业大会不知道大家关注到没有,看到消息是hanlp2.0版本发布了.不知道hanlp2.0版本又将带来哪些新的变化?准备近期看能够拿到一些hanlp2.0的资料,如果 ...

  3. linux命令(ubuntu18)记录...

    1.解压.zip文件unzip unzip studentCRUD-master.zip              2.读写权限chmod指令 r表是读 (Read) .w表示写 (Write) .x ...

  4. 洛谷P2178 [NOI2015]品酒大会 后缀数组+单调栈

    P2178 [NOI2015]品酒大会 题目链接 https://www.luogu.org/problemnew/show/P2178 题目描述 一年一度的"幻影阁夏日品酒大会" ...

  5. poj 2342 【Anniversary party】树形dp

    题目传送门//res tp poj 题意 给出一棵有权树,求一个节点集的权值和,满足集合内的任意两点不存在边 分析 每个点有选中与不选中两种状态,对于第\(i\)个点,记选中为\(sel_i\),不选 ...

  6. Pygame小游戏练习四

    @Python编程从入门到实践 Python项目练习 九.添加Play按钮 一.创建Button类 先让游戏一开始为非活动状态 # game_stats.py # --snip-- self.game ...

  7. k8s集群升级

    集群升级 由于课程中的集群版本是 v1.10.0,这个版本相对有点旧了,最新版本都已经 v1.14.x 了,为了尽量保证课程内容的更新度,所以我们需要将集群版本更新.我们的集群是使用的 kubeadm ...

  8. STM32之DMA实例

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/zouleideboke/article/details/75092558 DMA简介: DMA(Di ...

  9. Python 运算符与数据类型

    Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...

  10. Django的URLconf

    URL 概要 我们要在Django项目中为应用程序设计URL,我们可以创建一个名为URLconf(通常为urls.py)的Python模块.这个模块是纯Python代码,是一个简单的正则表达式到Pyt ...