作者:互联网
一. isinstance(obj,cls)
- 检查对象(obj) 是否是类(cls) 实例出来的
class Foo1:
def __init__(self):
self.name = "派大星"
class Foo2:
def __init__(self):
self.name = "海绵宝宝"
f1 = Foo1()
f2 = Foo2()
print(isinstance(f1,Foo1)) # True
print(isinstance(f2,Foo1)) # False
二. issubclass(sub,super)
- 检查sub类是否是super类的派生类
class Foo1:
def __init__(self):
self.name = "章鱼哥"
class Bar1(Foo1):
def run(self):
print("快跑")
class Bar2:
def run(self):
print("快跑")
print(issubclass(Bar1,Foo1)) # True
print(issubclass(Bar2,Foo1)) # False
标签:__,self,class,issubclass,print,isinstance,方法,Foo1,def 来源: https://www.cnblogs.com/songhaixing/p/14196935.html