(转)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) 在计算机中,整数的位数其实是有范围的,并没 ...
随机推荐
- wxpython 图像编程
转: http://wxhowto.googlecode.com/svn-history/r6/trunk/body/ch10.tex 的 HTML 档. 使用图像编程 这一章来了解一下我们可以使用图 ...
- hdu 4998 矩阵表示旋转
http://acm.hdu.edu.cn/showproblem.php?pid=4998 http://blog.csdn.net/wcyoot/article/details/33310329 ...
- underscore functions
// 创建一个用于设置prototype的公共函数对象 var ctor = function() {}; 1..bind(function, object, [*arguments]) :绑定fun ...
- Android-普通菜单Menu
第一种方式,使用Java方式 第二种方式,使用menu.xml package liudeli.activity; import android.app.Activity; import androi ...
- Svn在eclipse中使用
首先下载SvnAdt,我这里有个中文版的. 下载地址是 http://dl.vmall.com/c0i19tiqbq 你在其它地方下载的文件的话,解压文件后,把fea ...
- Argument list too long error for rm, cp, mv commands
Another option is to use find's -delete flag: find . -name "*.pdf" -delete
- The transaction associated with this command is not the connection's active transaction
The fix is fairly simple: if you want a Dapper query to participate in a connection, explicitly deno ...
- Python 数据结构与算法——冒泡排序
#方法一:递归 def bubble(lst,i): if i==1: return lst for j in range(i-1): if lst[j] > lst[j+1]: lst[j], ...
- 使用base64转码的方式上传图片
1.前端html代码 <input style="width:100%" onchange="loadpicture(1)" type="fil ...
- XML文件之创建
1.创建XML文档对象XmlDocument doc=new XmlDocument() 2.创建XML根节点变量XmlElement xmlElement 3.判断XML文件是否已经存在 1)若存在 ...