Python Programming – Special Functions in Python

The Python language provides two special functions, which can be used while using inheritance. These functions and their description is given in Table 11.1. The programming example for these functions is also given in Code 11.7., the output of which apparently makes clear the use of these functions.

Function

Description

issubclass(child, parent) Returns a boolean result; either true or false, true if the child is indeed a subclass of the superclass parent
isinstance(obj, Class) Returns a boolean result; either true or false, true if obj is indeed an instance of Class or a subclass of Class

 

Code: 11.7. Illustration of ininstance( ) and issubclass( ) functions.

#Illustration of ininstance( ) and issubclass( ) functions

class student: #base class
‘A student class’

class marks(student): #derived class
‘A marks class’

class result(marks): # new derived class
A result class’

r1=result()
r2=student()
print(issubclass(result, marks))
print(issubclass(student, marks))
print(isinstance(rl, result))
print(isinstance(r2, result))

Output

True
False
True
False

Python Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *