List of all base classes in a hierarchy of a given class?

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

+117
python inheritance oop class introspection
Sep 09 '09 at 19:41
source share
7 answers

inspect.getmro(cls) works with both the 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 to resolve the method.

 >>> class A(object): >>> pass >>> >>> class B(A): >>> pass >>> >>> import inspect >>> inspect.getmro(B) (<class '__main__.B'>, <class '__main__.A'>, <type 'object'>) 
+170
Sep 09 '09 at 20:28
source share

See the __bases__ property , available in the python class , which contains a tuple of base classes:

 >>> def classlookup(cls): ... c = list(cls.__bases__) ... for base in c: ... c.extend(classlookup(base)) ... return c ... >>> class A: pass ... >>> class B(A): pass ... >>> class C(object, B): pass ... >>> classlookup(C) [<type 'object'>, <class __main__.B at 0x00AB7300>, <class __main__.A at 0x00A6D630>] 
+38
09 Sep '09 at 19:48
source share

inspect.getclasstree() will create a nested list of classes and their bases. Using:

 inspect.getclasstree(inspect.getmro(IOError)) # Insert your Class instead of IOError. 
+30
Sep 09 '09 at 19:47
source share

you can use the __bases__ tuple of a __bases__ object:

 class A(object, B, C): def __init__(self): pass print A.__bases__ 

The tuple returned by __bases__ has all the base classes.

Hope this helps!

+21
Sep 09 '09 at 19:47
source share

Although Jochen’s answer is very useful and correct, since you can get the class hierarchy using the .ectmro () method of the inspect module, it’s also important to emphasize that the Python inheritance hierarchy is as follows:

eg:

 class MyClass(YourClass): 

Next class

  • Children class
  • Derived class
  • Subclass

eg:

 class YourClass(Object): 

Inherited class

  • Parent class
  • Base class
  • Superclass

One class can be inherited from another - class attributes are inherited - in particular, its methods are inherited - this means that instances of the inheriting (child) class can access the attribute of the inherited (parent) class

instance -> class -> then inherited classes

via

 import inspect inspect.getmro(MyClass) 

will show you the hierarchy inside Python.

+2
Feb 02 '18 at 15:38
source share

In python 3.7 you do not need to import inspect, type.mro will give you the result.

 >>> class A: ... pass ... >>> class B(A): ... pass ... >>> type.mro(B) [<class '__main__.B'>, <class '__main__.A'>, <class 'object'>] >>> 

Note that in Python 3.x, each class inherits from the base class of the object.

0
Jun 22 '19 at 6:26
source share

According to the Python documentation, we can also just use the class.__mro__ or class.mro() attribute:

 >>> class A: ... pass ... >>> class B(A): ... pass ... >>> B.__mro__ (<class '__main__.B'>, <class '__main__.A'>, <class 'object'>) >>> A.__mro__ (<class '__main__.A'>, <class 'object'>) >>> object.__mro__ (<class 'object'>,) >>> >>> B.mro() [<class '__main__.B'>, <class '__main__.A'>, <class 'object'>] >>> A.mro() [<class '__main__.A'>, <class 'object'>] >>> object.mro() [<class 'object'>] >>> A in B.mro() True 
0
Jul 10 '19 at 7:49
source share



All Articles