If I have Python code
class A(): pass class B(): pass class C(A, B): pass
and I have class C, is there a way to iterate through it’s super classed (A and B)? Something like pseudocode:
>>> magicGetSuperClasses(C) (<type 'A'>, <type 'B'>)
One solution seems to be inspect module and getclasstree function.
def magicGetSuperClasses(cls): return [o[0] for o in inspect.getclasstree([cls]) if type(o[0]) == type]
but is this a ‘Pythonian’ way to achieve the goal?
C.__bases__is an array of the super classes, so you could implement your hypothetical function like so:But I imagine it would be easier to just reference
cls.__bases__directly in most cases.