views:

314

answers:

4
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 ?

+2  A: 

I think you meant to use "class" instead of "def".. :) Anyway, try p2.__bases__

Joril
+10  A: 

Yes, there is way. You can use a issubclass function.

As follows:

class p1(object):pass
class p2(p1):pass

issubclass(p2, p1)
Serge
+2  A: 

Depending on what you're trying to do, the "mro" method can also be useful.

Azeem.Butt
+1  A: 

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

>>> class p1(object): pass
>>> class p2(p1): pass
>>> p2.__bases__
(<class '__main__.p1'>,)
-1: `issubclass` is certainly the much better solution.
nikow
http://docs.python.org/library/functions.html#issubclass
S.Lott