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. RHCE\RHCSA

    加油,老杨,所有的事情坚持到最后都是最好的,之所以现在觉得不好,是因为还没有坚持到最后,终于考过了,哈哈哈,下一个目标OCP

  2. 《C专家编程》读书笔记之第1~4章

    一.C:穿越时空的迷雾 1. C标准中定义了描述编译器的特点的一些术语: (1) 由编译器定义的(imprementation-defined) 由编译器设计者决定如何处理.例如:整型数右移时要不要扩 ...

  3. Shortest Unsorted Continuous Subarray

    Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...

  4. [转帖]三款Nehalem至强5500塔式服务器横评对决(4)

    三款Nehalem至强5500塔式服务器横评对决(4) http://tech.sina.com.cn/b/2009-12-14/05051172233_4.shtml 可以看到两路服务器的设置 基本 ...

  5. 添加 godoc 模块

    获取godoc源码go get -d golang.org/x/tools/cmd/godoc 或 go get golang.org/x/tools/cmd/godoc 如果 下不到源码,就用43服 ...

  6. notepad++一次多行复制粘贴到对应位置

    NodePad++一次复制多行粘贴到对应位置 有时候要写sql,但是里面有很多字段要对应上,如果要自己一个字段一个字段的去写是在有点麻烦,是不是有更好的方法做到这件事呢? 要做这件事,首先分析下我们已 ...

  7. 剑指offer12:求解double类型的浮点数base和int类型的整数exponent的次方。 保证base和exponent不同时为0

    1. 题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方.保证base和exponent不同时为0. 2. 思路和方法 分析: 由于 ...

  8. 微信小程序 路由跳转 异步请求 存储数据,微信登录接口

    1小程序路由跳转 wx.switchTab(Object object) 这里的tabBar是底下的导航栏指定的页面 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面 tabBar l ...

  9. Python 运算符与数据类型

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

  10. 一个复杂关联的sql

    在项目中遇到了一个比较复杂关系的sql,关联关系有些模糊,现在梳理一下 sql如下: SELECT TRAN.TRANS_DATE, TRAN.TRANS_TIME, TRAN.BUSI_TRAC_C ...