Write a program to check whether the object is of a class or its subclass.
Answer Posted / 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 View All Answers
Write a program to convert all strings in list to integers?
Is it possible to inherit one class from another class?
Explain “with” statement in python?
What is monkey Patching in python?
Write python logic to count the number of capital letters in a file?
How do you remove duplicates from a list in python whilst preserving order?
Which module is used to control the gpio in python?
What mode is used for both writing and reading in binary format in file.?
What is built-in type in python?
Do I need to learn c++ before python?
What is index out of range error?
Do you know the number of keywords in python?
What is a variable in python?
What is the purpose of "self" in python
Is python an oop?