# 给动态生产的类定义一个方法 def __init__(self,name): self.name = name print(self.name) def take(self,obj): print(obj) # 动态生成一个类type('类名',(父类1,父类2,),{字典:属性或方法}) Dog = type('Dog',(object,),{'age':22,'__init__':__init__,'take':take}) d = Dog('wdc') d.take('www') #调用类中的方法
结果:
isinstance方法:
class Foo(object): pass obj = Foo() # 判断obj是否是类Foo的对象,返回True或False a = isinstance(obj,Foo) print(a)
issubclass方法:
class Foo(object): pass class Bar(Foo): pass # 判断Bar是不是Foo的子类,返回True或False a = issubclass(Bar,Foo) print(a)