Python2和python3中类型判断

  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中没有区别

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值