(转)python 判断数据类型
原文:https://blog.csdn.net/mydriverc2/article/details/78687269
Python 判断数据类型有type和isinstance
基本区别在于:
type():不会认为子类是父类
isinstance():会认为子类是父类类型
|
1
2
3
4
5
6
7
8
9
|
class Color(object): passclass Red(Color): passprint type(Color()) == Colorprint type(Red()) == Colorprint isinstance(Red(),Color) |
执行结果如下:
|
1
2
3
4
|
D:\software\Python2.7.13\python.exe C:/Users/Administrator/PycharmProjects/PythonStudy/test.pyTrueFalseTrue |
用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 判断数据类型的更多相关文章
- python 判断数据类型及释疑
Python 判断数据类型有type和isinstance 基本区别在于: type():不会认为子类是父类 isinstance():会认为子类是父类类型 class Color(object): ...
- Python输入数据类型判断正确与否的函数大全(非常全)
对于python输入数据类型判断正确与否的函数大致有三类: (1)type(),它的作用直接可以判断出数据的类型 (2)isinstance(),它可以判断任何一个数据与相应的数据类型是否一致,比 ...
- python 基本数据类型分析
在python中,一切都是对象!对象由类创建而来,对象所拥有的功能都来自于类.在本节中,我们了解一下python基本数据类型对象具有哪些功能,我们平常是怎么使用的. 对于python,一切事物都是对象 ...
- python常用数据类型内置方法介绍
熟练掌握python常用数据类型内置方法是每个初学者必须具备的内功. 下面介绍了python常用的集中数据类型及其方法,点开源代码,其中对主要方法都进行了中文注释. 一.整型 a = 100 a.xx ...
- python之数据类型详解
python之数据类型详解 二.列表list (可以存储多个值)(列表内数字不需要加引号) sort s1=[','!'] # s1.sort() # print(s1) -->['!', ' ...
- python基本数据类型之集合
python基本数据类型之集合 集合是一种容器,用来存放不同元素. 集合有3大特点: 集合的元素必须是不可变类型(字符串.数字.元组): 集合中的元素不能重复: 集合是无序的. 在集合中直接存入lis ...
- python基本数据类型之字符串(四)
python基本数据类型之字符串(四) 判断方法 python中有一类用来判断字符串形式的方法,该类方法有两个特点:(1)方法名都是is开头(除了startswith和endswith):(2)返回值 ...
- python基本数据类型之字符串(三)
python基本数据类型之字符串(三) 转换和判断方法 在python中,有一些内置方法可以将字符串转化特定形式,而与之对应的一些方法可以判断字符串是否符合某些形式.因此,在这篇文章中,笔者把转换方法 ...
- Python的数据类型和运算
一.Python数据类型 变量所指向的值是有自己独特的数据类型的,这些数据类型可能代表不同的数据,在Python中,主要有以下几种数据类型: 整形(int) 在计算机中,整数的位数其实是有范围的,并没 ...
随机推荐
- 在windows7下创建ftp服务站点
1.开始->控制面板->程序(点击“卸载程序”)->启动或关闭windows功能->Internet Information Services(Internet信息服务)-&g ...
- angularjs写公共方法
'use strict'; angular.module('fast-westone') .factory('commonUtilService', function () { return { /* ...
- ArcGIS Desktop Python add-ins 共享和安装插件
1) 共享和安装插件 共享Python插件的关键是.esriaddin文件;为了获取该插件功能,其他用户只要在本机执行安装操作或通过网络引用该插件就可以. ArcGIS插件安装工具 当用户双击一个 ...
- 简单的cxf-rs
整体结构 │ pom.xml │ └─src ├─main │ ├─java │ │ └─cn │ │ └─zno │ │ HelloWorld.java │ │ InputBean.java │ │ ...
- vs2015 cmd.exe已退出 代码为1
https://blog.csdn.net/changbin91/article/details/42874377?utm_source=blogxgwz0 https://blog.csdn.net ...
- (匹配)The Accomodation of Students --HDU --2444
链接: http://acm.hdu.edu.cn/showproblem.php?pid=2444 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- hdu 1000 真水题
#include <iostream> #include <cstdio> #include <cstring> #include <string.h> ...
- Flord算法传递闭包
POJ3660 对于flord算法得学习,这篇博客写的非常好http://blog.csdn.net/ljhandlwt/article/details/52096932 这个题问你给你n头牛得前后关 ...
- 菜鸟——springboot+mybatis+maven
网上找了很多资料,学习如何搭建springboot,由于刚刚接触springboot,不是很熟练,通过参考网上别人搭建的例子,自己也搭建了一个简单的springboot+mybaits+maven 网 ...
- 使用WebService与Oracle EBS进行集成
http://www.cnblogs.com/isline/archive/2010/04/15/1712428.html 一.概述 OracleEBS是Oracle公司的ERP产品,这个产品非常庞大 ...