List all base classes in a hierarchy of given class?

后端 未结 7 1020
清歌不尽
清歌不尽 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:43

    In python 3.7 you don't need to import inspect, type.mro will give you the result.

    >>> class A:
    ...   pass
    ... 
    >>> class B(A):
    ...   pass
    ... 
    >>> type.mro(B)
    [, , ]
    >>>
    

    attention that in python 3.x every class inherits from base object class.

提交回复
热议问题