The isinstance() function returns true if the first argument of the function is a subclass of the second argument. Actually, we can say that this function is used to check the given class is a subclass of the other class.
issubclass(class, classinfo) #where class specify the class name
bytes() Parameters:
Takes 2 parameters. We can also pass a tuple of classes as the classinfo argument, in that case, the function will return True if the class is a subclass of any of the classes in the tuple.
Parameter | Description | Required / Optional |
---|---|---|
class | class to be checked | Required |
classinfo | class, type, or tuple of classes and types | Required |
It returns a boolean value either true or false.
Input | Return Value |
---|---|
a class is a subclass | true |
a class is not a subclass | false |
In any other case | TypeError exception |
class Polygon:
def __init__(polygonType):
print('Polygon is a ', polygonType)
class Triangle(Polygon):
def __init__(self):
Polygon.__init__('triangle')
print(issubclass(Triangle, Polygon))
print(issubclass(Triangle, list))
print(issubclass(Triangle, (list, Polygon)))
print(issubclass(Polygon, (list, Polygon)))
Output:
True False True True
print('bool is the subclass of int: ', issubclass(bool, int))
print('float is the subclass of int: ', issubclass(float, int))
print('str is the subclass of list: ', issubclass(str, list))
import collections
print('collections.OrderedDict is the subclass of dict: ', issubclass(collections.OrderedDict, dict))
Output:
bool is the subclass of int: True float is the subclass of int: False str is vsubclass of list: False collections.OrderedDict is the subclass of dict: True
print(issubclass(bool, (int, str, list)))
print(issubclass(float, (int, str, list))
Output:
True False