Python Glossary Русский

all Python 3inspectfunction

inspect.getmro()

members and modules

A tuple of base classes in attribute lookup order (MRO), starting with the class itself; the same as cls.__mro__.

Syntax

inspect.getmro(cls) -> tuple[type, ...]

It takes a class, not an instance: getmro(obj) fails with AttributeError, you need getmro(type(obj)). With diamond inheritance the order is not 'the whole left branch first': the common ancestor comes after all its subclasses (D, B, C, A), and super() picks the next method along exactly this chain. So super() in B may call a method of C that B knows nothing about.

Examples

import inspect
class A: pass
class B(A): pass
class C(A): pass
class D(B, C): pass
print([c.__name__ for c in inspect.getmro(D)])  # → ['D', 'B', 'C', 'A', 'object']
print(inspect.getmro(D) == D.__mro__)  # → True
inspect.getmro(D())  # → AttributeError: 'D' object has no attribute '__mro__'

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy