How the Python issubclass() Method works?

The Issubclass() Method In Python

Hey folks! Today we are here with yet another tutorial on the topic Python issubclass() method.

So let us get right into the topic.

What is Python issubclass()?

The Python issubclass() method is a built-in method that checks whether the passed class is a subclass of the specified classinfo(could be a class or a tuple of class objects). It returns True if the class is a subclass (direct, indirect or virtual) of classinfo. Or else, returns False. In any other case, a TypeError exception is raised.

issubclass(class, classinfo)

class is the name of the class that we want to confirm is a subclass of classinfo or not. If classinfo is a tuple of classes, the function checks the same for each one of the classes present in it.

So, now that we have a basic idea of what issubclass() does, let us look at some examples for getting a better understanding.

Using the Python issubclass() Method

In this section, we are going to be dealing with the different ways we can use the Python issubclass() method. For both the cases where we pass classinfo as a class name as well as a tuple of class objects.

1. Python issubclass() In Inheritance

When we are talking about Inheritance, the concept of parent and child classes is very crucial. When a child inherits from a parent class, it derives or extends the properties of it. At this point, child is said to be a subclass of the parent class.

Hence, the issubclass() method comes handy when we want to get information on a class’s inheritance. Or check if it inherits from another class or not.

So let us take an example.

class Animals:
    pass

class Humans(Animals):
    pass

class Men(Humans):
    pass

class Women(Humans):
    pass

print("issubclass(Women, Humans) :", issubclass(Women, Humans))
print("issubclass(Men, Humans) :", issubclass(Men, Humans))
print("issubclass(Women, Men) :", issubclass(Women, Men)) #both are child of the same Humans Class
print("issubclass(Humans, Animals) :", issubclass(Humans, Animals))
print("issubclass(Animals, Humans) :", issubclass(Animals, Humans)) #not the opposite
print("issubclass(Women, Animals) :", issubclass(Women, Animals)) #multi-level inheritance
print("issubclass(Animals, object) :", issubclass(Animals, object)) #object is the baseclass of all classes

Output:

Python Issubclass Output
issubclass() with Inheritance – Output

Here Animals is the parent class with a child Humans. Two classes, Men and Women again inherit from the Humans class(multi-level Inheritance). Hence both the Men and Women class are subclasses of Humans as well as Animals. And Humans is a subclass of Animals.

This explains for the top 6 results.

Further for the last case, we get True. This is because of the fact that all classes in Python are subclasses of the object class.

2. Python issubclass() With tuple of classes

Now let us try considering classinfo as a tuple of classes. Look at the example below carefully.

class Animals:
    pass

class Humans(Animals):
    pass

class Men(Humans):
    pass

class Women(Humans):
    pass

print("issubclass(Men, (str, list, tuple)) :", issubclass(Men, (str, list, tuple)))
print("issubclass(Men, (str, list, Humans)) :", issubclass(Men, (str, list, Humans)))
print("issubclass(Women, (str, list, Women)) :", issubclass(Women, (str, dict, Women)))
#A class is a subclass of itself in Python

Output:

Issubclass With Tuple Of Classes
issubclass() with Tuple Of Classes – Output

Again here we have taken the same Inheritance example. As mentioned earlier, this time classinfo is passed as a tuple of class objects. In that case, the function checks whether the passed class is a subclass of any one of the class objects in the classinfo tuple.

For the very first result, Men is obviously not a subclass of any of str, list, or tuple classes. So we get a False.

For the next case, Men is again not a subclass of str or list but, is a subclass of Humans. Hence, we get True as a result.

Lastly, for the last case, we get True since Women is a subclass of itself. This is because of the fact that in Python every class is a subclass of itself.

Conclusion

So that’s it for today. We learned about the Python issubclass() method.

For any further questions, feel free to write it in the comments below.

References