Issubclass Function in Python

In Python, the issubclass function is used to determine if one class is a subclass of another.

Syntax

issubclass(subclass, base_class)

The function takes two arguments: the subclass (the derived class) and the base_class (the parent class).

It returns a boolean value:

  • True if the first class is a subclass of the second.
  • False if it is not.

    A Practical Example

    Here’s a simple script with two classes:

    The class Regioni is defined as a subclass of the class Nazioni.

    1. class Nazioni:
    2. nazione = "Italia"
    3. class Regioni(Nazioni):
    4. regione = "Lazio"

    To check whether Regioni is a subclass of Nazioni, we can use the issubclass() function.

    The function outputs True:

    >>> issubclass(Regioni, Nazioni)
    True

    This confirms that Regioni is indeed a subclass of Nazioni.

    And that’s it!

     

     

     
     

    Please feel free to point out any errors or typos, or share suggestions to improve these notes. English isn't my first language, so if you notice any mistakes, let me know, and I'll be sure to fix them.

    FacebookTwitterLinkedinLinkedin
    knowledge base

    Python Classes