Python Reference Library →

Python issubclass() Function – Check Class Inheritance

Let’s look at issubclass() function – check class inheritance

  1. Notice

    issubclass() checks a class’s inheritance relationship. A class is also considered a subclass of itself.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
class Parent:
    pass
class Child(Parent):
    value = 3
print(issubclass(Child, Parent))
print(issubclass(Child, Child))
Live state

Top-level scope

Parent→Object 1
Child→Object 3
Object 1 · typeclass ParentObject 3 · typeclass Child
This step

issubclass() checks a class’s inheritance relationship. A class is also considered a subclass of itself.

Picture issubclass() function – check class inheritance

issubclass() checks a class’s inheritance relationship. A class is also considered a subclass of itself.

Image brief

Alt text
A diagram for Python issubclass() Function – Check Class Inheritance. Instructions: class Parent: pass class Child(Parent): value = 3 print(issubclass(Child, Parent)) print(issubclass(Child, Child)). Displayed result: True True. The final example bindings are Parent --> object 1 (type); Child --> object 3 (type). object 1: class Parent; object 3: class Child
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python issubclass() Function – Check Class Inheritance". Teaching focus: issubclass() checks a class’s inheritance relationship. A class is also considered a subclass of itself. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS class Parent: pass class Child(Parent): value = 3 print(issubclass(Child, Parent)) print(issubclass(Child, Child)) | v DISPLAYED RESULT True True FINAL BINDINGS Parent --> object 1 (type) Child --> object 3 (type) object 1: class Parent object 3: class Child Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python issubclass() Function – Check Class Inheritance. Instructions: class Parent: pass class Child(Parent): value = 3 print(issubclass(Child, Parent)) print(issubclass(Child, Child)). Displayed result: True True. The final example bindings are Parent --> object 1 (type); Child --> object 3 (type). object 1: class Parent; object 3: class Child

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
class Parent:
    pass
class Child(Parent):
    value = 3
print(issubclass(Child, Parent))
print(issubclass(Child, Child))
Live state

Top-level scope

Parent→Object 1
Child→Object 3
Object 1 · typeclass ParentObject 3 · typeclass Child
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
True
True

What do you expect this program to print?

What does this example display?

class Parent:
    pass
class Child(Parent):
    value = 3
print(issubclass(Child, Parent))
print(issubclass(Child, Child))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

True True

Follow the printed values in order. The output is: True True

More examples and reference

Python issubclass() Function

The issubclass() function checks if a class is a subclass of another class or a tuple of classes. It is used in object-oriented programming to verify inheritance relationships.

Syntax

issubclass(class, classinfo)

Parameters:

  • class – The class you want to check.
  • classinfo – A class or a tuple of classes to check against.

Returns:

  • True if class is a subclass (direct or indirect) of classinfo.
  • False otherwise.

Example 1: Basic Usage

class Animal:
    pass

class Dog(Animal):
    pass

print(issubclass(Dog, Animal))
True

Example 2: Indirect Subclass

class Animal:
    pass

class Mammal(Animal):
    pass

class Cat(Mammal):
    pass

print(issubclass(Cat, Animal))
True

Why? Because Cat → Mammal → Animal. So, Cat is an indirect subclass of Animal.

Example 3: Using Tuple

class Vehicle: pass
class Car(Vehicle): pass

print(issubclass(Car, (Vehicle, Animal)))
True

Returns True if Car is a subclass of any class in the tuple.

Example 4: Invalid Check

print(issubclass(5, int))
TypeError: issubclass() arg 1 must be a class

Note: You must pass a class as the first argument, not an instance.

Use Cases

  • Validating object types in class hierarchies
  • Conditional behavior in frameworks using inheritance
  • Metaprogramming and decorators

Common Mistakes

  • ❌ Passing an instance instead of a class: issubclass(dog, Animal) ❌
  • Instead, use: isinstance(dog, Animal) to check instances

Interview Tip

issubclass() is often tested with isinstance(). Know the difference: one checks classes, the other checks instances.

Summary

  • issubclass(A, B) returns True if A is a subclass of B
  • Supports tuples of classes
  • Raises TypeError if the first argument is not a class

Practice Problem

Create a class hierarchy with Shape, Polygon, and Triangle. Use issubclass() to check if Triangle is a subclass of Shape.

class Shape: pass
class Polygon(Shape): pass
class Triangle(Polygon): pass

print(issubclass(Triangle, Shape))

Expected Output:

True

What to remember

Connect what you noticed in the example with the idea below.

Remember

issubclass() checks a class’s inheritance relationship. A class is also considered a subclass of itself.

Watch out for this idea

issubclass() accepts an ordinary instance as its first argument.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →