Issubclass Function In Python

Overview:

  • A subclass in Python specializes one or more base classes. The subclass derives certain functionalities from the base classes but it has its own special features than that of the base class.
  • If the subclass B is derived from class A, B is a direct subclass of A.
  • If class C is derived from class B which is derived from class A, then class C is an indirect subclass of B.
  • If X is a class that has specialised methods which a developer deems fit to consider as a classification under the class A(given A is an abstract class), then this classification is called virtual inheritance in Python. 
  • Given a class name the built-in function issubclass() determines whether it is a subclass of another class. The subclass could be a direct, indirect or a virtual subclass.

Example 1 - Checks a direct subclass is derived from a base class:

# Example Python program that checks whether a direct subclass is 
# a subclass of a given baseclass

# class definition of Rover
class Rover:
    def __init__():
        pass
        
    def move():
        print("Rover moving")

    def turn(direction):
        print("Rover turning")
    
    def applybreak():
        print("Applying break")
    
    def on():
        print("Rover turning on")
    
    def off():
        print("Rover turning off")

# class defintion of ForestRover 
class ForestRover(Rover):
    def __init__(self, version):
        self.version = version 
        self.roverOn = False

    def move(self, direction):
        if False is self.roverOn:
            print("Start the rover first")
            return
        print("Forest rover moving in %s"%direction)
    
    def turn(self, direction):
        print("Forest rover turning %s"%direction)
    
    def applybreak(self):
        print("Forest rover applying break")
    
    def on(self):
        if self.roverOn is False:
            self.roverOn = True
        print("Forest rover is on")
    
    def off(self):
        if self.roverOn is True:
            self.roverOn = False
        print("Forest rover is off")

# Create an instance of a ForestRover
rover   = ForestRover(1.0)

# Check whether ForestRover is a subclass of Rover
isRover = issubclass(ForestRover, Rover)
print("Forest rover is derived from Rover:%s"%isRover);

# Perform some rover operations 
rover.on();
rover.move("forward");
rover.turn("right");
rover.off();

Output:

Forest rover is derived from Rover:True

Forest rover is on

Forest rover moving in forward

Forest rover turning right

Forest rover is off

Example 2 - Checks an indirect subclass is derived from a base class:

class MarshlandRover(ForestRover):
    def float(self):
        print("Floating")
        
# Check whether MarshlandRover is a subclass of Rover
isRover = issubclass(ForestRover, Rover)
print("MarshlandRover is derived from Rover:%s"%isRover)

mlRover = MarshlandRover(1.0)
mlRover.float()

Output:

MarshlandRover is derived from Rover:True

Floating

Example 3 - Checks whether a virtual subclass is a subclass through issubclass():

# Example Python program that checks whether
# a virtual subclass is a subclass of a given class
from abc import ABC
from abc import abstractmethod

# Create an abstract base class (i.e., A rover interface) 
class Rover(ABC):
    @abstractmethod
    def move():
        pass

    @abstractmethod
    def turn(direction):
        pass

    @abstractmethod
    def applybreak():
        pass

    @abstractmethod
    def on():
        pass

    @abstractmethod
    def off():
        pass

# An independent mars rover not part of rover hierarchy
class MarsRover:
    def move():
        print("moving")

    def turn(direction):
        print("turning")

    def applybreak():
        print("breaking")

    def on():
        print("swithcing on")

    def off():
        print("swithcing off")

    def collectSample():
        print("collecting sample")

# An independent mars water rover not part of rover hierarchy
class MarsWaterRover(MarsRover):
    def float():
        print("Floating");
    
isARover = issubclass(MarsRover, Rover)
print("Is MarsRover a Rover:%s"%isARover)

# Make the MarsRover part of Rover hierarchy
Rover.register(MarsRover)

isARover = issubclass(MarsRover, Rover)
print("Is MarsRover a Rover:%s"%isARover)

# Since MarsRover has been registered in the rover hierarchy
# mars water rover aslo becomes part of rover hierarchy
isARover = issubclass(MarsWaterRover, Rover)
print("Is MarsWaterRover a Rover:%s"%isARover)

Output:

Is MarsRover a Rover:False

Is MarsRover a Rover:True

Is MarsWaterRover a Rover:True

 


Copyright 2020 © pythontic.com