列出给定类层次结构中的所有基类? [英] List all base classes in a hierarchy of given class?

查看:90
本文介绍了列出给定类层次结构中的所有基类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个类 Foo (无论它是新风格类,如何生成所有基类 - 在继承层次结构中的任何地方 - 它 issubclass

Given a class Foo (whether it is a new-style class or not), how do you generate all the base classes - anywhere in the inheritance hierarchy - it issubclass of?

推荐答案

inspect.getmro(cls)适用于新旧样式类并返回与 NewClass.mro()相同的:类及其所有祖先类的列表,按照用于方法解析的顺序。

inspect.getmro(cls) works for both new and old style classes and returns the same as NewClass.mro(): a list of the class and all its ancestor classes, in the order used for method resolution.

>>> class A(object):
>>>     pass
>>>
>>> class B(A):
>>>     pass
>>>
>>> import inspect
>>> inspect.getmro(B)
(<class '__main__.B'>, <class '__main__.A'>, <type 'object'>)

这篇关于列出给定类层次结构中的所有基类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆