上面三个方法具体区别请看下面代码:
""" !/usr/bin/env python -*- coding: utf-8 -*- @Time : 2021/1/23 10:20 @Author : xibo.zhu @Site : @File : 获取父类名称、所有父类名称.py @Software: PyCharm """ class Grandpa(object): pass class Father01(Grandpa): pass class Father02(Grandpa): pass class Myself(Father01,Father02): def get_father_class(self): ''' 获取当前类名称,获取当前类的所有父类名称 :return: ''' # 获取当前类名称 myself_calss_name = self.__class__.__name__ print(f'本类名称是:{myself_calss_name}') # 获取当前类父类名称 father_class_objs = self.__class__.__bases__ # 返回一个迭代器 father_class_name = [father_class_obj.__name__ for father_class_obj in father_class_objs] print(f'当前类的所有父类名称:{father_class_name}') def get_ancestor_names(self): ''' 获取基础的父类,父类的父类 ... 意思就是获取所有祖先 :return: ''' # 获取所有祖先类 ,返回一个迭代器 ancestor_objs = self.__class__.mro() ancestor_names = [one_ancestor_obj.__name__ for one_ancestor_obj in ancestor_objs] print(f'当前类的所有祖先类是:{ancestor_names}') if __name__ == '__main__': obj = Myself() obj.get_father_class() print("======================") obj.get_ancestor_names()