更新时间:2023-02-23 19:12:37
你想使用小部件.__子类__()
获取所有子类的列表。它只查找直接子类,但如果你想要所有这些,你将不得不做更多的工作:
You want to use Widget.__subclasses__()
to get a list of all the subclasses. It only looks for direct subclasses though so if you want all of them you'll have to do a bit more work:
def inheritors(klass):
subclasses = set()
work = [klass]
while work:
parent = work.pop()
for child in parent.__subclasses__():
if child not in subclasses:
subclasses.add(child)
work.append(child)
return subclasses
NB如果您使用的是Python 2.x,则仅适用于新式类。
N.B. If you are using Python 2.x this only works for new-style classes.