Python issubclass() Function

The issubclass() function in Python is used when we need to check whether a class is a sub-class of another class or not. For example:

class CodesCracker:
    Name = "Sophia"
    Course = "EECS"
    Age = "20"

class obOne(CodesCracker):
    Name = CodesCracker
    City = "Liverpool"

if issubclass(obOne, CodesCracker):
    print("'obOne' is a sub-class of 'CodesCracker'")
else:
    print("'obOne' is not a sub-class of 'CodesCracker'")

Because obOne is created as a sub-class of CodesCracker. Therefore the output will be:

'obOne' is a sub-class of 'CodesCracker'

Python issubclass() Function Syntax

The syntax of issubclass() function in Python, is:

issubclass(class, classinfo)

where class is a class that has to be check whether it is a sub-class of classinfo or not. The classinfo refers to a class, types, or a tuple of classes and/or types.

Note: The function issubclass() returns True, if first argument (class) is a sub-class of second argument (classinfo). Otherwise returns False.

Python issubclass() Function Example

Here is an example of issubclass() function in Python:

class CodesCracker:
    Name = "Sophia"
    Course = "EECS"
    Age = "20"

class obOne(CodesCracker):
    Name = CodesCracker
    City = "Liverpool"

print(issubclass(obOne, CodesCracker))
print(issubclass(CodesCracker, obOne))
print(issubclass(obOne, list))
print(issubclass(obOne, (list, CodesCracker)))

The output will be:

True
False
False
True

Python Online Test


« Previous Function Next Function »


Liked this post? Share it!