Get a list of bases in an object
How do you get a list of all the bases used by an object in its creation? For example:
class MixinA(object):
pass
class MixinB(object):
pass
class mixedClass(MixinB, MixinA, object):
pass
my_object = mixedClass()
bases_used = my_object.??? # Is there a property that contains a list of [MixinA, MixinB, object]?
I am looking to somehow retrieve what is based on an object, primarily because I am actually building the class of the object dynamically, but want to communicate which mixins were used in certain error cases.
+3
source to share
2 answers
see inspect.getmro or inspect.getclasstree
>>> inspect.getmro(type(my_object))
(<class '__main__.mixedClass'>, <class '__main__.MixinB'>, <class '__main__.MixinA'>, <class 'object'>)
>>> type(my_object).__bases__
(<class '__main__.MixinB'>, <class '__main__.MixinA'>, <class 'object'>)
+2
source to share