您的位置:首页 > 编程语言 > Python开发

python类型判断

2015-11-18 14:19 671 查看
python语言提供了type函数来判断传入数据的类型,我们可以利用函数来做类型判断。

例如:

class A:
a=1

if type(5) == int:
print "This is int"

if type("xxx") == str:
print "This is str"

if type(['a','b']) == list:
print "This is list"

if type({'a':1,'b':2}) == dict:
print "This is dict"

This is int
This is str
This is list
This is dict


如果不知道类型是什么名字,直接type出来。

class A:
    pass
b=A()
print type(A)
print type(b)
结果:
<type 'classobj'>
<type 'instance'>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: