Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

python

What is the issubclass() method in Python?

Educative Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The issubclass() method is used to determine if a class is a subclass of a specified class.

  • It returns True if a class is a subclass of the specified class
  • It returns False if a class is not a subclass of the specified class
Function signature

classinfo can be specified as a tuple of class objects. In that case, every entry in classinfo is checked. isubclass() returns True if class is a subclass of any element in the tuple.

Code

In the code below, we check if the Kiwi class is a subclass of the Fruit class. Since it is, issubclass() returns True.

class Fruit:
  def __init__(fruittype):
    print(fruittype)

class Kiwi(Fruit):
  def __init__(self):
    Fruit.__init__('Kiwi')
    
print(issubclass(Kiwi, Fruit))

In the following code snippet, we check if Kiwi is a subclass of Fruit and Vegetable. Since Kiwi is a subclass of Fruit, issubclass() returns True.

class Fruit:
  def __init__(fruittype):
    print(fruittype)

class Vegetable:
  def __init__(vegetabletype):
    print(fruittype)

class Kiwi(Fruit):
  def __init__(self):
    Fruit.__init__('Kiwi')
    
print(issubclass(Kiwi, (Fruit, Vegetable)))

RELATED TAGS

python
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring