Checking if A is superclass of B in Python

Andz picture Andz · Dec 21, 2009 · Viewed 14k times · Source
class p1(object): pass
class p2(p1): pass

So p2 is the subclass of p1. Is there a way to find out programmatically that p1 is [one of] the superclass[es] of p2 ?

Answer

user235859 picture user235859 · Dec 21, 2009

using <class>.__bases__ seems to be what you're looking for...

>>> class p1(object): pass
>>> class p2(p1): pass
>>> p2.__bases__
(<class '__main__.p1'>,)