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 fresherearth:
    Name = "Sophia"
    Course = "EECS"
    Age = "20"

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

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

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

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

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 fresherearth:
    Name = "Sophia"
    Course = "EECS"
    Age = "20"

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

print(issubclass(obOne, fresherearth))
print(issubclass(fresherearth, obOne))
print(issubclass(obOne, list))
print(issubclass(obOne, (list, fresherearth)))

The output will be:

True
False
False
True

Python Online Test


« Previous Function Next Function »