• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Check if Class is Subclass in Python with issubclass()

Check if Class is Subclass in Python with issubclass()

July 25, 2022 Leave a Comment

To check if a class is a subclass in Python, the easiest way is with the issubclass() function.

class Fruit:
    pass

class Apple(Fruit):
    pass

print(issubclass(Apple,Fruit))
print(issubclass(Fruit,Apple))

#Output:
True
False

When working in Python, the ability to perform certain checks in our program is valuable.

One such check is if you want to check if a class is a subclass in Python.

To check if a class is a subclass in Python, the easiest way is with the issubclass() function.

issubclass() allows you to check if a class is a subclass of another class (or tuple of classes) or not.

issubclass() returns True if the given class is the subclass of given class else it returns False.

Below is a simple example showing you how to check if a class is a subclass or not using Python.

class Fruit:
    pass

class Apple(Fruit):
    pass

class GrannySmith(Apple):
    pass

print(issubclass(Apple,Fruit))
print(issubclass(Fruit,Apple))
print(issubclass(GrannySmith,Apple))
print(issubclass(Apple,GrannySmith))
print(issubclass(Fruit,GrannySmith))
print(issubclass(GrannySmith,Fruit))

#Output:
True
False
True
False
False
True

Hopefully this article has been useful for you to learn how to check if a class is a subclass of another class in Python.

Other Articles You'll Also Like:

  • 1.  How to Split List in Half Using Python
  • 2.  pandas mean – Get Average of Series or DataFrame Columns
  • 3.  Convert String to Integer with int() in Python
  • 4.  pandas idxmin – Find Index of Minimum Value of Series or DataFrame
  • 5.  Python atanh – Find Hyperbolic Arctangent of Number Using math.atanh()
  • 6.  How to Convert pandas Column dtype from Object to Category
  • 7.  Selenium minimize_window() Function to Minimize Window in Python
  • 8.  Find Median of List in Python
  • 9.  How to Write Python Dictionary to File
  • 10.  Check if String Does Not Contain Substring in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy