# 从当前类出发到top类,中间有多少继承的类 def findbases(kls, topclass): retval = list() for base in kls.__bases__: if issubclass(base, topclass): retval.extend(findbases(base, topclass)) retval.append(base) # return list(set(retval)) # 觉得用set去除重复可能比较好 return retval
测试代码:
class People(object): def __init__(self): self.head_number=1 self.hand_number=2 self.foot_number=2 self.eye_number=2 self.nose_number=1 def get_head_numer(self): return self.head_numer def get_hand_number(self): return self.hand_number def get_foot_number(self): return self.foot_number def get_eye_number(self): return self.eye_number def get_nose_number(self): return self.nose_number def can_act(self): print("people can eat") print("people can smile") print("people can speak") class Teacher(People): def __init__(self): super(Teacher,self).__init__() def can_act(self): print("teacher can teach") class Student(People): def __init__(self): super(Student,self).__init__() def can_act(self): print("sudent is learning****") class Son(Student): def __init__(self): super(Son,self).__init__() def can_act(self): print(" son can learn from parents") class GrandSon(Son,Student,Teacher): def __init__(self): super(GrandSon,self).__init__() def can_act(self): print(" GrandSon can learn from parents")
findbases(GrandSon,People)