List all base classes in a hierarchy of given class?

后端 未结 7 1021
清歌不尽
清歌不尽 2020-11-28 02:28

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 -

7条回答
  •  感情败类
    2020-11-28 02:53

    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)
    (, , )
    

提交回复
热议问题