博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python2和python3中类型判断
阅读量:4287 次
发布时间:2019-05-27

本文共 2736 字,大约阅读时间需要 9 分钟。

  1. Python2类型判断

>>> type(33) == types.IntType

True

>>> type("33") == types.StringType

True

>>> type(33.33) == types.FloatType

True

>>> type({"dd":33}) == types.DictType

True

>>> type([33,33]) == types.ListType

True

>>> type((33,33)) == types.TupleType

True

>>> type({"33","22"}) == set  #没有types.SetType类型

True

>>> class A(object):

...     pass

...

>>> type(A()) == types.ObjectType

False

>>>type(22) == int

True

>>> type("test") == str

True

>>> type(22.33) == float

True

>>> type({"a":2}) == dict

True

>>> type((3,32,2)) == tuple

True

>>> type([2,2,3,4]) == list

True

>>> class A(object):

...     pass

...

>>> type(A()) == A

True

>>> type(A()) == object  # 不会认为子类是一种父类类型

False

>>> isinstance(A(), object)   # 会认为子类是一种父类类型

True

>>> isinstance(22,int)

True

>>> isinstance("33", str)

True

>>> isinstance(22.22, float)

True

>>> isinstance({"aa":22}, dict)

True

>>> isinstance([22,22,33], list)

True

>>> isinstance((33,33,44), tuple)

True

>>> isinstance({"33","34"},set )

True

>>> class A:

...     pass

...

>>> isinstance(A(), A)

True

>>> isinstance(A(), object)

True

>>> type(A()) == object

False

>>> type(A()) == A

False

>>> type(A()) == types.ObjectType  #  不会认为子类是一种父类类型

False

结论:尽量不要使用type()方法,多使用isinstance()来判断数据类型,

type不会任务子类是一种父类类型,而isinstance认为子类是一种父类

 

2.python3类型判断

C:\Users\Administrator>python

Python 3.6.5 4

Type "help", "copyright", "credits" or "license" for more in

>>> import types

>>> type(33) == types.IntType  # python3中不支持该类型判断方式

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: module 'types' has no attribute 'IntType'

>>> type(33) == int

True

>>> type("33") == str

True

>>> type(33.33) == float

True

>>> type([3,3,3]) == list

True

>>> type({"a":23}) == dict

True

>>> type((33,3)) == tuple

True

>>> type({"33","22"}) == set

True

>>> isinstance({"33","34"},set )

True

>>> isinstance(22, int)

True

>>> isinstance(22.33, float)

True

>>> isinstance("aa", str)

True

>>> isinstance({"aa":33}, dict)

True

>>> isinstance([22,23], list)

True

>>> isinstance((22,23), tuple)

True

>>> class A(object):

...     pass

...

>>> type(A()) == A

True

>>> type(A()) == object

False

>>> type(A()) == types.ObjectType

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

AttributeError: module 'types' has no attribute 'ObjectType'

>>> isinstance(A(), A)

True

>>> isinstance(A(), object)

True

结论:Python3中,类型判断不能使用types.IntType等方式判断类型,尽量使用isinstance来判断类型type不会任务子类是一种父类类型,而isinstance认为子类是一种父类

3.字符串类型判断

前提:变量Str_1为字符串

Str_1.isalnum()   所有字符都是数字或者字母,

Str_1.isalpha()   所有字符都是字母

Str_1.isdigit()   所有字符都是数字

Str_1.islower()   所有字符都是小写

Str_1.isupper()   所有字符都是大写

Str_1.istitle()    所有单词都是首字母大写

Str_1.isspace()   所有字符都是空白字符

以上方法为真返回 Ture,否则返回 False

例如:

>>> Str_1 = "333"

>>> Str_1.isdigit()

True

>>>

结论:以上字符类型判断在python2和python3中没有区别

转载地址:http://szagi.baihongyu.com/

你可能感兴趣的文章
几行代码看懂android View的事件传递机制(视图逻辑)
查看>>
android Handler的核心原理,核心代码关键词
查看>>
图片处理的几种算法(毛玻璃效果,高斯模糊效果,旧时光效果,lomo效果,暖意效果)
查看>>
Android打包,如何打包时不包含依赖包(gradle各种依赖方式)
查看>>
RTMP协议
查看>>
HTTP协议详解-简要
查看>>
MQTT协议通俗讲解
查看>>
XMPP协议分析-简要版
查看>>
mqtt实现库对比
查看>>
动态权限申请,android7.08.0权限特征
查看>>
android虚拟键盘判断和高度获取代码
查看>>
android_WebView截图
查看>>
android-加固方案对比
查看>>
android状态栏黑色字体,时间电池深色
查看>>
一段比较坑的求职经历-from android little guy.
查看>>
Weex学习 and Kotlin学习
查看>>
几种直播流媒体协议
查看>>
ExoPlayer
查看>>
流媒体框架
查看>>
java语言当中-sleep()和await()的差异
查看>>