List all base classes in a hierarchy of given class?

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

    Although Jochen's answer is very helpful and correct, as you can obtain the class hierarchy using the .getmro() method of the inspect module, it's also important to highlight that Python's inheritance hierarchy is as follows:

    ex:

    class MyClass(YourClass):
    

    An inheriting class

    • Child class
    • Derived class
    • Subclass

    ex:

    class YourClass(Object):
    

    An inherited class

    • Parent class
    • Base class
    • Superclass

    One class can inherit from another - The class' attributed are inherited - in particular, its methods are inherited - this means that instances of an inheriting (child) class can access attributed of the inherited (parent) class

    instance -> class -> then inherited classes

    using

    import inspect
    inspect.getmro(MyClass)
    

    will show you the hierarchy, within Python.

提交回复
热议问题