December 14, 2022

issubclass() in Python With Examples

The Python issubclass() is a built-in function that checks whether the passed class is a subclass of the specified another class or not.

Syntax of the Python issubclass() is as given below-

issubclass(class, classinfo)

Function returns True if class is a subclass (direct, indirect or virtual) of classinfo.

classinfo may be a class or a tuple of class objects, if it is a tuple of class objects then class (first argument) is checked against each entry in the tuple.

Python issubclass() examples

1. As obvious you will be using issubclass() function when you are using inheritance in Python. Here is a hierarchical structure where Employee class inherits from Person and Manager class inherits from Employee, so we have Multi-level inheritance scenario here. Let’s check different combinations using issubclass().

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def display_info(self):
    print('Name:', self.name)
    print('Age:', self.age)


class Employee(Person):
  def __init__(self, emp_id, department, name, age):
    super().__init__(name, age)
    self.emp_id = emp_id
    self.department = department

  def display_info(self):
    super().display_info()
    print('Id:', self.emp_id)
    print('Department:', self.department)


class Manager(Employee):
  def __init__(self, perks, emp_id, department, name, age):
    super().__init__(emp_id, department, name, age)
    self.perks = perks

  def display_info(self):
    super().display_info()
    print('Perks:', self.perks)


print("issubclass(Employee, Person):", issubclass(Employee, Person))
print("issubclass(Manager, Person):", issubclass(Manager, Person))
print("issubclass(Manager, Employee):", issubclass(Manager, Employee))
print("issubclass(Employee, Manager):", issubclass(Employee, Manager))
print("issubclass(Person, Employee):", issubclass(Person, Employee))
Output
issubclass(Employee, Person): True
issubclass(Manager, Person): True
issubclass(Manager, Employee): True
issubclass(Employee, Manager): False
issubclass(Person, Employee): False

Manager indirectly inherits from Person too so it is of type Person also that’s why issubclass(Manager, Person) returns true. Vice-versa is not true though Manager is not subclass of Employee, Person is not a sub-class of Employee too so both these function calls return false.

2. In this example we’ll pass a tuple as second argument in issubclass() function.

class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

  def display_info(self):
    print('Name:', self.name)
    print('Age:', self.age)


class Employee(Person):
  def __init__(self, emp_id, department, name, age):
    super().__init__(name, age)
    self.emp_id = emp_id
    self.department = department

  def display_info(self):
    super().display_info()
    print('Id:', self.emp_id)
    print('Department:', self.department)


class Manager(Employee):
  def __init__(self, perks, emp_id, department, name, age):
    super().__init__(emp_id, department, name, age)
    self.perks = perks

  def display_info(self):
    super().display_info()
    print('Perks:', self.perks)


print("issubclass(Employee, (str, list))):", issubclass(Employee, (str, list)))
print("issubclass(Employee, (str, list, Person)):", issubclass(Employee, (str, list, Person)))
print("issubclass(Manager, (str, list)):", issubclass(Manager, Employee))
print("issubclass(Person, (Employee, Manager)):", issubclass(Person, (Employee, Manager)))
print("issubclass(Person, (Employee, Manager, object)):", issubclass(Person, (Employee, Manager, object)))
Output
issubclass(Employee, (str, list))): False
issubclass(Employee, (str, list, Person)): True
issubclass(Manager, (str, list)): True
issubclass(Person, (Employee, Manager)): False
issubclass(Person, (Employee, Manager, object)): True

First call obviously returns false as Employee is not a subclass of either str or list.

Second is true because Employee is subclass of Person and one of the entry in the tuple passes the check.

Last call issubclass(Person, (Employee, Manager, object)) returns true because object is the super class of all classes in Python.

That's all for the topic issubclass() in Python With Examples. If something is missing or you have something to share about the topic please write a comment.


You may also like

No comments:

Post a Comment