type和isinstance区别

class Foo(object):
    pass

class Bar(Foo):
    pass

obj = Bar()

# isinstance用于判断,对象是否是指定类的实例 (错误的)
# isinstance用于判断,对象是否是指定类或其派生类的实例
print(isinstance(obj,Foo))#True
print(isinstance(obj,Bar))#True


print(type(obj) == Bar)#True
print(type(obj) == Foo)#False

# 对象,判断是否是某个类型? type,isinstance

你可能感兴趣的:(type和isinstance区别)