isinstance(obj,cls)和issubclass(sub,super)
isinstance
isinstance(obj,cls)检查是否obj是否是类 cls 的对象
判断第一个参数是不是第二个参数的对象,返回true或者false
class Foo:
pass
class Bar:
pass
f=Foo()
print(isinstance(f,Foo))
print(isinstance(f,Bar))
True
False
issubclass
issubclass(sub, super)检查sub类是否是 super 类的派生类
判断第一个类是不是第二个类的子类,返回true或者false
class Foo:
pass
class Bar(Foo):
pass
class Tr(Bar):
pass
print(Bar.__bases__)
print(issubclass(Bar,Foo))
print(issubclass(Tr,object))
(<class '__main__.Foo'>,)
True
True