day26 Python isinstance和issubclass
isinstance(obj,cls)检查是否obj是否是类 cls 的对象
issubclass(sub, super)检查sub类是否是 super 类的派生类
class Foo(object): pass f1=Foo() print(isinstance(f1,Foo)) class Foo(object): pass class Bar(Foo): pass print(issubclass(Bar,Foo)) 结果: True True
补充
#_*_coding:utf-8_*_ __author__ = 'Linhaifeng' class A: pass class B(A): pass print(issubclass(B,A)) #B是A的子类,返回True a1=A() print(isinstance(a1,A)) #a1是A的实例,返回True 结果: True True