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, ...))
.
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.
Arguments | class | Class to be checked for being a subclass. |
class or tuple of classes | Class or tuple of classes the first argument should be checked against. | |
Return Value | Boolean | Returns 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?
Check out my 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).
Publisher 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!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. 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?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming 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.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.