python - isinstance/issubclass 函数

#isinstance(obj,cls)
#检查是否obj是否是object的类cls的对象
#判断一个对象是否是一个类的实例

class F00(object):
    pass

obj = F00()

print(isinstance(obj, F00))
#返回 True

#issubclass(sub,super)
#判断是否是对象的子类

class F02(F00):
    pass

print(issubclass(F02,F00))

 

isinstance(obj,cls)应用:

#拓展(判断一个数据的数据类型)
a = 'aaaa'
b = [1,2,3,4]
c = (1,2,3)
d = {"a":1}
e = 233

l = [a,b,c,d,e]

for i in l:
    print(isinstance(i, str))

 

posted @ 2018-10-13 22:13  Anec  阅读(145)  评论(0编辑  收藏  举报