Python Tutorial Python Advanced Python References Python Libraries

Python issubclass() Function



The Python issubclass() function is used to check whether the specified class is a subclass (derived class) of the another specified class or not. It returns true when the specified specified class is a subclass (derived class) of the another specified class, else returns false.

Syntax

issubclass(class, classinfo)

Parameters

class Required. Specify the class which need to be checked.
classinfo Required. Specify type or class, or tuple of types or classes.

Example:

In the example below, issubclass() function is used to check the class called rectangle is a subclass of class called polygon or not.

class polygon:
  def __init__(self, length, breadth):
    self.length = length
    self.breadth = breadth

class rectangle(polygon):
  pass

x = issubclass(rectangle, polygon)
print(x)

The output of the above code will be:

True

Example:

Instead of specifying one class or type, a tuple of types or classes can be used. In the example below, issubclass() function is used to check the class called rectangle is a subclass of any of the classes present in the given tuple or not.

class polygon:
  def __init__(self, length, breadth):
    self.length = length
    self.breadth = breadth

class rectangle(polygon):
  pass

x = issubclass(rectangle, (list, tuple, set))
print(x)

x = issubclass(rectangle, (list, tuple, set, polygon))
print(x)

The output of the above code will be:

False
True

❮ Python Built-in Functions

5