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 ?
using <class>.__bases__ seems to be what you're looking for...
>>> class p1(object): pass
>>> class p2(p1): pass
>>> p2.__bases__
(<class '__main__.p1'>,)
__bases__
will only give you the direct parents (the ones in the parenthesis of the class definition), while issubclass checks the entire chain of inheritance.
Yes, there is way. You can use a issubclass function.
As follows:
class p1(object):pass
class p2(p1):pass
issubclass(p2, p1)
issubclass(A, A)
evaluates to True
. This may not be intuitive. At least to me, it seems obvious that a class is not its own subclass. issubclass
should have been called istypeofclass
.
set
, this doesn't bother me. A is a subset of A.
Mar 13, 2015 at 11:06
issubclass(A,B) and not A == B
Oct 4, 2022 at 8:09