1、继承关系
# -*- coding: utf-8 -*- class Animal(object): pass class Cat(Animal): pass class Dog(Animal): pass cat = Cat()
2、子类判断
issubclass(x, A_tuple)
print(issubclass(Cat, Animal)) # True print(issubclass(Cat, Dog)) # False # 两种方式等价 print(issubclass(Cat, (Animal, Dog))) # True print(issubclass(Cat, Animal) or issubclass(Cat, Dog)) # True
3、实例判断
isinstance(x, A_tuple)
print(isinstance(cat, Animal)) # True print(isinstance(cat, Cat)) # True print(isinstance(cat, Dog)) # False # 两种方式等价 print(isinstance(cat, (Cat, Dog))) # True print(isinstance(cat, Cat) or isinstance(cat, Dog)) # True
4、获取类的子类
# -*- coding: utf-8 -*- class A(object): pass class B(A): pass class C(A): pass for clazz in A.__subclasses__(): print(clazz.__name__) # B C