Python issubclass()

Python issubclass() function checks if a class is a subclass of another class. It helps verify inheritance relationships, ensuring a class inherits properties and methods from a parent class. Python issubclass() returns True if the inheritance is valid, making it useful for validating class hierarchies in object-oriented programming.
Table of Contents

Understanding Python issubclass() Function

The issubclass() function in Python is a built-in function used to determine if one class is a subclass of another. You provide two classes as arguments. Python issubclass() returns True if the first class is a subclass of the second, and False otherwise. This function is essential for checking class hierarchies and ensuring your code interacts with classes as expected. It helps maintain the integrity of object-oriented designs.

Syntax of Python issubclass()

result = issubclass(class, classinfo)

Explanation

  • result: Variable to store boolean result.
  • issubclass(): Built-in function to check subclass relationship.
  • class: The class which is needed to be checked if it is a subclass.
  • classinfo: Class, or tuple of classes, to check against.

Example of Python issubclass()

class Animal:
    pass

class Dog(Animal):
    pass

print(issubclass(Dog, Animal))

Explanation

  • class Animal:: Defines base class.
  • class Dog(Animal):: Defines Dog as a subclass of Animal.
  • print(issubclass(Dog, Animal)): Checks if Dog is a subclass of Animal.

Output

True


issubclass() Parameters

The issubclass() function takes two parameters. The first parameter, class, is the class you want to check for being a subclass. The second parameter, classinfo, can be a single class or a tuple of classes. If classinfo is a tuple, Python issubclass() checks if the first parameter (class) is a subclass of any of the classes in the tuple. This can be useful to check multiple possible parent classes at once.

Syntax

is_sub = issubclass(potential_subclass, class_or_tuple_of_classes)

Explanation

  • is_sub: Stores returned value.
  • issubclass(): Checks relationship between the given class and the class_or_tuple_of_classes.
  • potential_subclass: Class which is needed to be checked.
  • class_or_tuple_of_classes: Single class or a tuple of classes.

Example

class A:
    pass

class B(A):
    pass

class C:
    pass

print(issubclass(B, (A, C)))

Explanation

  • class A:: Defines a class.
  • class B(A):: Defines B class, inheriting from A.
  • class C:: Defines another class.
  • print(issubclass(B, (A, C))): Checks if B is a subclass of either A or C.

Output

True


issubclass() Return Value

Python issubclass() function provides a clear boolean answer: True or False. It returns True if the first class provided is a subclass of the second class (or any class within the second parameter if it’s a tuple). If the first class is not a subclass, or if either of the provided arguments is not a class, the return value will be False, or an exception may be raised respectively.

Syntax

boolean_value = issubclass(class1, class2)

Explanation

  • boolean_value: Holds result, True or False.
  • issubclass(): The function call.
  • class1: Class to check for being a subclass.
  • class2: Class (or tuple of classes).

Example

class X:
    pass

class Y:
    pass

print(issubclass(X, Y))

Explanation

  • class X:: Defines class.
  • class Y:: Defines another class.
  • print(issubclass(X, Y)): Checks X is a subclass of Y.

Output

False


Single Class Inheritance Check

The most basic use of Python issubclass() is to check a direct parent-child relationship between two classes. You provide the potential subclass as the first argument and the potential superclass as the second. This confirms the inheritance structure of simple class hierarchies. This is fundamental to understanding and validating object relationships in your code.

Syntax

is_direct_subclass = issubclass(child_class, parent_class)

Explanation

  • is_direct_subclass: Stores the result.
  • issubclass(): Checks the subclass relationship.
  • child_class: Class is checked to be a subclass.
  • parent_class: Class to be checked for superclass.

Example

class Vehicle:
    pass

class Car(Vehicle):
    pass

print(issubclass(Car, Vehicle))

Explanation

  • class Vehicle:: Base class.
  • class Car(Vehicle):: Car inherits from Vehicle.
  • print(issubclass(Car, Vehicle)): Checks if Car is subclass of Vehicle.

Output

True


Multiple Class Inheritance Check

Python supports multiple inheritance, meaning a class can inherit from multiple parent classes. Python issubclass() can also be used to check these relationships. You can check if the first class argument is a class of the second class argument, which is a tuple of classes. It will check for all the classes in a tuple, and if the class is a subclass of any of the classes in a tuple, it returns True; otherwise, it returns False.

Syntax

is_sub = issubclass(child, (Parent1, Parent2, Parent3))

Explanation

  • is_sub: Holds the returned boolean value.
  • issubclass(): Built-in function to check relationship.
  • child: Class is checked to be a subclass.
  • (Parent1, Parent2, Parent3): Tuple of possible parent classes.

Example

class A: pass
class B: pass
class C(A, B): pass

print(issubclass(C, (A, B)))

Explanation

  • class A: pass: Defines class A.
  • class B: pass: Defines class B.
  • class C(A, B): pass: C inherits from both A and B.
  • print(issubclass(C, (A, B))): Checks if C is a subclass of any class from the tuple (A,B).

Output

True


issubclass() with Complex Inheritance Chains

In large object-oriented projects, you’ll often encounter complex inheritance hierarchies, with classes inheriting from classes that inherit from other classes, and so on. Python issubclass() works perfectly in these situations. It correctly checks the subclass relationship regardless of how many levels deep the inheritance goes. It traverses the entire inheritance chain.

Syntax

is_related = issubclass(subclass, ancestor_class)

Explanation

  • is_related: Will store the True or False result.
  • issubclass(): Checks for subclass relationship between classes.
  • subclass: Class is checked for being subclass.
  • ancestor_class: Class which might be an ancestor.

Example

class Grandparent:
    pass

class Parent(Grandparent):
    pass

class Child(Parent):
    pass

print(issubclass(Child, Grandparent))

Explanation

  • class Grandparent:: The base class.
  • class Parent(Grandparent):: Parent inherits from Grandparent.
  • class Child(Parent):: Child inherits from Parent.
  • print(issubclass(Child, Grandparent)): Checks if Child is a subclass of Grandparent.

Output

True


Handling Exceptions in Inheritance Checks

It’s important to remember that Python issubclass() expects classes as its arguments. If you pass something that isn’t a class (like an integer, string, or an instance of a class), you’ll get a TypeError. Therefore, if you are uncertain about the types you’re working with, it’s good practice to use a try…except block to gracefully handle potential TypeError exceptions.

Syntax

try:
    is_sub = issubclass(maybe_class, another_maybe_class)
except TypeError:
    # Handle the error
    pass

Explanation

  • try...except: Handles potential errors.
  • is_sub = issubclass(maybe_class, another_maybe_class): Might raise a TypeError.
  • except TypeError: Catches TypeError if it happens.
  • pass: Placeholder error-handling action.

Example

try:
    result = issubclass(5, int)
    print(result)
except TypeError as e:
    print(e)

Explanation

  • try...except: Handles potential error.
  • result = issubclass(5, int): Attempts to use issubclass with an integer, it will show error.
  • print(result): Will not executed.
  • except TypeError as e: Catches TypeError and assigns to e.
  • print(e): Print information about error.

Output

issubclass() arg 1 must be a class


Conclusion

Python issubclass() is an important built-in function in Python. You learned that the function takes class and classinfo as parameters. The return value will be either True or False based on whether the given class is a subclass of classinfo. If the provided arguments are not class, then TypeError will be raised.


Also Read

Python int()

Python iter()


Python Reference

Python issubclass()