Technology3 min read

Python issubclass() Method with Examples

Abhishek Ahlawat

By Abhishek Ahlawat — Recently

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

#Python#Programming

Summarize this article using AI

The issubclass() is a Python built-in function that checks if one of the classes(which is passed as a parameter) is a subclass of a specific class/classes or not.

The syntax of the function is:

issubclass(class_to_be_checked, class_with_which_it_is_checked)

The first parameter is the class which needs to be compared and the second parameter is the class/tuple/tuple of classes/type against which the first parameter has to be compared.

The second parameter needn't necessarily be a class. As mentioned above, it could be a class, tuple, tuple of classes or a type. If anything apart from this is passed, an exception TypeError is raised.

Return type: It returns true when the class belongs to a specific class and returns false if it doesn't belong to the specified class.

Let's take a simple, but weird example,

class Subject:
    # some data and functions

issubclass(Subject, Subject)

Output:

True

In the code above, we have called the issubclass() function for the Subject class to check if it is a subclass of itself. And surprisingly the output came out to be True.

In Python, the general notion is that a class is a subclass of itself. Well, the above example was just to introduce you with a python rule. Now let's take an example.




Time for an Example

class Subject:
    def __init__(subject):
        print("The Subject is related to", subject)

class Trigonometry(Subject):
    def __init__(self):
        Subject.__init__(‘sin_theta’)

# simple use of issubclass
print(issubclass(Trigonometry, Subject))
# checking against a tuple with multiple types listed
print(issubclass(Trigonometry, (list, Subject)))
# checking against a datatype
print(issubclass(Trigonometry, list))
# checking against a tuple with multiple types listed
print(issubclass(Subject, (list, Subject)))

Output:

True
True
False
True

In the code above we have a class Subject and another class Trigonometry which is inheriting the class Subject, which means it is a subclass of the Subject class.

The first output is true since Trigonometry is a subclass of Subject.

The second output is true since Trigonometry is a subclass of the type Subject and we were comparing it against a tuple with multiple different types provided, one was a list type and other was class Subject. Now because Trigonometry is a subclass of the Subject class hence the function call returned true.

Which tells us more about this function, that if we provide multiple classes to check against, then even if one of them is the parent class, this will return true.

Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops — put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next