What is the purpose of the isinstance() function in Python?
Python > Python Classes and Objects > Python Introduction to OOP
256
Answer:
The isinstance()
function in Python is used to determine if an object is an instance of a particular class or of a subclass of that class. The function takes two arguments: an object and a class or a tuple of classes. The function returns True
if the object is an instance of the class or a subclass of the class, and False
otherwise.
Here's an example of using the isinstance()
function in Python:
class MyClass: pass obj = MyClass() if isinstance(obj, MyClass): print("obj is an instance of MyClass") else: print("obj is not an instance of MyClass")
In this example, the isinstance()
function is used to determine if obj
is an instance of the MyClass
class. If obj
is an instance of MyClass
, the message "obj
is an instance of MyClass
" is printed. Otherwise, the message "obj
is not an instance of MyClass
" is printed.
This Particular section is dedicated to Question & Answer only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.
Join Our telegram group to ask Questions
Click below button to join our groups.