如何找到给定名称的类的所有子类?我需要一种工作方法来获取从Python中的基类继承的所有类。
3 回答
Qyouu
TA贡献1786条经验 获得超11个赞
.__subclasses__()
def get_all_subclasses(cls): all_subclasses = [] for subclass in cls.__subclasses__(): all_subclasses.append(subclass) all_subclasses.extend(get_all_subclasses(subclass)) return all_subclasses
HUX布斯
TA贡献1876条经验 获得超6个赞
def get_subclasses(cls): for subclass in cls.__subclasses__(): yield from get_subclasses(subclass) yield subclass
@classmethoddef get_subclasses(cls): for subclass in cls.__subclasses__(): yield from subclass.get_subclasses() yield subclass
添加回答
举报
0/150
提交
取消