Check If a Class is a Subclass in Python



We have the classes A and B defined as follows −

class A(object): pass
class B(A): pass

B can be proved to be a sub class of A in two ways as follows

class A(object):pass
class B(A):pass
print issubclass(B, A) # Here we use the issubclass() method to check if B is subclass of A
print B.__bases__ # Here we check the base classes or super classes of B

This gives the output

True
(<class '__main__.A'>,)
Updated on: 2020-06-13T13:52:45+05:30

369 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements