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'>