Python issubclass()

Python’s built-in issubclass(X, Y) function takes a class X and a class Y and returns True if the X is an instance of Y and otherwise False. The argument Y can also be a tuple in which case it checks whether X is a subclass of any class in the tuple—such as in issubclass(X, (class_1, class_2, ...)).

Python issubclass() Function - Example + Definition

Usage

Learn by example! Here’s an example on how to use the issubclass() built-in function.

class Car:
    pass

class Porsche(Car):
    speed = 200 #mph


print('Is Porsche a subclass of Car?')
print(issubclass(Porsche, Car))

The output is:

Is Porsche a subclass of Car?
True

Porsche is a subclass of Car because you set the inheritance hierarchy in the class definition with Porsche(Car). Porsche inherits ever method and attribute from its parent Car—so the function issubclass(Porsche, Car) returns True.

If you pass a non-parent of Porsche, the function will return False:

print(issubclass(Porsche, int))
# False

Video issubclass()

Syntax issubclass()

The issubclass() method has the following syntax:

Syntax: 
issubclass(class, class)         # Check if a class is a subclass of another class. 
issubclass(class, (class_A, class_B, ...))         # Check if a class is a subclass of any of multiple other classes.
ArgumentsclassClass to be checked for being a subclass.
class or tuple of classesClass or tuple of classes the first argument should be checked against.
Return ValueBooleanReturns whether the class is a subclass of any of the classes given in the second argument.

Return value from issubclass()

The issubclass(class, classes) method returns True if the class is a subclass of any of the classes given in the second argument. Otherwise, it returns False.

Interactive Shell Exercise: Understanding issubclass()

Consider the following interactive code:

Exercise: One of the outputs is False. Which one?


But before we move on, I’m excited to present you my brand-new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners


Python issubclass() vs isinstance()

Python has two similar but different built-in functions issubclass() and isinstance().

  • The function issubclass() checks whether a class is a subclass of another class.
  • The function isinstance() checks if an object is an instance of a class.

So, the difference between issubclass() and isinstance() is that the former is about the relationship between two classes and the latter is about the relationship between an instance and a class.

Here’s an example:

class Car:
    pass

class Porsche(Car):
    speed = 200 #mph

print(issubclass(Porsche, Car))
# True

your_car = Porsche()
print(isinstance(your_car, Car))
# True1

Summary

Python’s built-in issubclass(X, Y) function takes a class X and a class Y and returns True if the X is an instance of Y and otherwise False.

class Car:
    pass

class Porsche(Car):
    speed = 200 #mph

print(issubclass(Porsche, Car))
# True

The argument Y can also be a tuple in which case it checks whether X is a subclass of any class in the tuple—such as in issubclass(X, (class_1, class_2, ...)).

print(issubclass(Porsche, (int, float, Car)))
# True

I hope you enjoyed the article! To improve your Python education, you may want to join the popular free Finxter Email Academy:

Do you want to boost your Python skills in a fun and easy-to-consume way? Consider the following resources and become a master coder!

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!