How I can get a list with all superclasses of given class in Python?
I know, there is a __subclasses__()
method in inspent
module for getting all subclasses, but I don't know any similar method for getting superclasses.
Use the __mro__
attribute:
>>> class A:
... pass
...
>>> class B:
... pass
...
>>> class C(A, B):
... pass
...
>>> C.__mro__
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)
This is a special attribute populated at class instantiation time:
class.__mro__
This attribute is a tuple of classes that are considered when looking for base classes during method resolution.
class.mro()
This method can be overridden by a metaclass to customize the method resolution order for its instances. It is called at class instantiation, and its result is stored in__mro__
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With