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

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

问题描述

给定一个类 Foo(是否是一个 new-style 类与否),如何生成所有基类 - 继承层次结构中的任何位置 - 它issubclass of?

解决方案

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

<预><代码>>>>A类(对象):>>>经过>>>>>>B(A)类:>>>经过>>>>>>进口检验>>>检查.getmro(B)(<class '__main__.B'>, <class '__main__.A'>, <type 'object'>)

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) 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天全站免登陆