Tuesday 9 November 2021

Python: issubclass: Check whether this class is subclass of other class or not

Signature

issubclass(class, classinfo)

 

This method return True if class is a subclass (direct, indirect or virtual) of classinfo, else False. A class is considered a subclass of itself.

 

issubclass_1.py

from collections import namedtuple

Employee = namedtuple("Employee", "id age name")

print('issubclass(Employee, tuple) -> ' + str(issubclass(Employee, tuple)))
print('issubclass(Employee, Employee) -> ' + str(issubclass(Employee, Employee)))
print('issubclass(Employee, list) -> ' + str(issubclass(Employee, list)))

 

Output

issubclass(Employee, tuple) -> True
issubclass(Employee, Employee) -> True
issubclass(Employee, list) -> False

 

‘classinfo’ may be a tuple of class objects. ‘issubclass(x, (A, B))’ is equivalent to issubclass(x, A) or issubclass(x, B)

 

issubclass_2.py

 

from collections import namedtuple

Employee = namedtuple("Employee", "id age name")

print('issubclass(Employee, (tuple, Employee)) -> ' + str(issubclass(Employee, (tuple, Employee))))
print('issubclass(Employee, (tuple, Employee, list)) -> ' + str(issubclass(Employee, (tuple, Employee, list))))

 

Output

issubclass(Employee, (tuple, Employee)) -> True
issubclass(Employee, (tuple, Employee, list)) -> True

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment