Methods and Functions

Python issubclass ()

Python issubclass () is a function that works on the concept of inheritance. Python is an object-oriented language, in which, every entity that is defined in Python is treated as an object. The concept of inheritance is one of the most essential concepts in object-oriented programming. Inheritance refers to the ability of one class in a program to derive the properties that are attributed to any other class in the program. 

Through inheritance, the code written can be reused, thereby increasing the functionality of the program. Through inheritance, one can add other features to a class without changing its attributes as well.  The Python issubclass () is a built-in function that is used to check whether a class is the subclass of another class in the program or not. 

issubclass () Parameters 

The parameters that are passed in the issubclass () include object and class information. The syntax is as follows.

issubclass (object, classinfo)

Here, object refers to the name of the class that needs to be checked and classinfo refers to the class, tuples, or types of classes that are present in the class mentioned. 

Return Value from issubclass ()

The issubclass () function returns either of the two values: True or False. If it returns true, that means that the class passed in the parameter is a subclass of the required class. If it is false, then it is not.  

Example: How does issubclass () work?

The program below tests whether the class that is passed in the Python issubclass () function is a subclass of the class or is any element of the tuple present in the parent class. 

                    

class Equations:

              def __init__ (equationType):

                             print (“The equation is “, equationType)

class Quadratic (Equations):

              def __init__ (self):

                             Equation.__init__(“Quadratic”)

print (issubclass (Quadratic, Equation))

print (issubclass (Quadratic, list))

print (issubclass (Quadratic, (list, Equation)))

print (issubclass (Quadratic, (list, Equation)))

The output of the program above will be as follows. 

True 

False 

True 

True 

 

Note that as the class Quadratic is not an element of the list, the function issubclass () returns a false value. Here, in the last two lines of the code, tuples are used to specify multiple classinfo names. The function can also be used to check if any class is the subclass of a user-defined class.  

Another thing that one should keep in mind is that the function issubclass () works differently from the function isinstance (). These methods are considerably similar. The function isinstance () checks whether the object that is passed in its parameters is the subclass of the classinfo mentioned within the parameters. 

On the other hand, the Python issubclass () function only checks whether a class is a subclass of a supposed parent class.    

Frequently Asked Questions

Is Python a subclass?

A subclass in Python is based on the concept of inheritance in Python. It is a class that inherits or derives all the properties of its parent class. The class that inherits its properties from a parent or superclass is also known as a child class or heir class.  

What does isinstance mean in Python?

The isinstance () function is a built-in function in Python that is used to check whether any instance or object in Python is the subclass of the class mentioned in the classinfo. 

What does super () __Init__ do in Python?

With the use of the super () function, the child class can access the init () property of the parent class. Thus, the super () function can be used to build classes that extend their functionality to a previously built class without implementing it again.  

How do I get a list of built-in functions in Python? 

One can use the built-in keyword to access all the built-in functions in Python. 

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

avatar
  Subscribe  
Notify of

Customize your course in 30 seconds

Which class are you in?
No thanks.