Python issuclass() method

The issubclass() method checks if the specified class is the subclass of the specified classinfo argument.

Syntax:

issubclass(class, classinfo)

Parameters:

  1. class: The class to be checked as a subclass of classinfo.
  2. classinfo: The class name as a string or a tuple of classes to be considered as base classes.

Return Value:

Returns True if the specified class is the subclass of the classinfo argument, otherwise returns False.

The following checks the subclass of the built-in classes.

Example: issubclass()
print('bool is the subclass of int: ', issubclass(bool, int))
print('float is the subclass of int: ', issubclass(float, int))
print('str is the subclass of list: ', issubclass(str, list))

import collections
print('collections.OrderedDict is the subclass of dict: ', issubclass(collections.OrderedDict, dict))
Output
bool is the subclass of int: True
float is the subclass of int: False
str is vsubclass of list: False
collections.OrderedDict is the subclass of dict: True

In the above example, issubclass(bool, int) returns True because the bool class is a subclass of int. Likewise, it checks for other built-in classes.

The issubclass() method can also use a tuple to specify multiple classinfo names, as shown below.

Example: issubclass()
print(issubclass(bool, (int, str, list)))
print(issubclass(float, (int, str, list))
Output
True
False

Use the issubclass() method to check the subclass of user-defined classes, as shown below.

Example: issubclass() Check User-defined Classes
class employee:
    def __init__(employeetype):
        print("Employee is a ",employeetype)

class HR(employee):
    def __init__(self):
        employee.__init__('HR')

print('HR is subclass of employee: ', issubclass(HR, employee))
print('HR is subclass of list: ', issubclass(HR, list))
print('HR is subclass of list or employee: ', issubclass(HR, (list, employee)))
print('HR is subclass of HR: ', issubclass(HR, HR))
Output
HR is subclass of employee: True
HR is subclass of list: False
HR is subclass of list or employee: True
HR is subclass of HR: True

Note that the class is considered to be a subclass of itself e.g. issubclass(int, int) returns True.