from collections.abc import Iterable, Iterator lst = [] # isinstance 这个内置函数可以帮我们判断xxx类是否是yyy类型的子类 print(isinstance(lst, Iterable)) print(isinstance(lst.__iter__(), Iterator)) class Animal(object): pass class Cat(Animal): pass class Doraemon(Cat): pass cat = Cat() doraemon = Doraemon() print(isinstance(cat, Animal)) # 判断猫是否是动物类 True print(isinstance(doraemon, Animal)) # 判断机器猫是否是动物类 True print(isinstance(doraemon, Cat)) # 判断机器猫是否是猫类 True print(isinstance(cat, Doraemon)) # 判断猫是否是机器猫类 False # issubclass 判断某类是否是其他类的子类 print(issubclass(Cat, Animal)) # 判断猫类是否是动物类 True print(issubclass(Doraemon, Cat)) # 判断机器猫是否是猫类 True print(issubclass(Doraemon, Animal)) # 判断机器猫是否是动物类 True