Write a program to check whether the object is of a class or its subclass.



Write a program to check whether the object is of a class or its subclass...

Answer / chaitanya

There is a method which is built-in to show the instances of an object that consists of many classes by providing a tuple in a table instead of individual classes. The method is given as isinstance(obj,cls) and in more details given as:

isinstance(obj, (class1, class2, ...)) that is used to check about the object’s presence in one of the classes. The built in types can also have many formats of the same function like isinstance(obj, str) or isinstance(obj, (int, long, float, complex)).

It is not preferred to use the class instead user-defined classes are made that allow easy object-oriented style to define the behavior of the object’s class. These perform different thing that is based on the class. The function differs from one class to another class. To find out the object of the particular class the following program is used:

def search(obj):

if isinstance(obj, box):

# This is the code that is given for the box and write the program in the object

elif isinstance(obj, Document):

# This is the code that searches the document and writes the values in it

elif

obj.search()

#This is the function used to search the object’s class.

Is This Answer Correct ?    2 Yes 0 No

Post New Answer

More Python Interview Questions

What is python? What do you understand of pep 8?

0 Answers  


How can you generate random numbers in python?

0 Answers  


How do you include a module in your python file?

0 Answers  


How to create dynamic array in python?

0 Answers  


What is Python Set?

0 Answers  






Explain how you can access sessions in flask?

0 Answers  


Is string empty python?

0 Answers  


How do I exit python?

0 Answers  


How variables are declared in python?

0 Answers  


Does python have private?

0 Answers  


How do you calculate percentiles with python/ numpy?

0 Answers  


Which methods of python are used to determine the type of instance and inheritance?

0 Answers  


Categories