Python isinstance() method
The python isinstance() method returns True if the specified object is an instance or subclass; otherwise, it will return False.
Python isinstance() method
The syntax of isinstance() is:
1
2
isinstance(object, class)
isinstance() Parameters
isinstance() method takes two parameters as arguments:
- object - Name of the object to be checked
- class - Type of the class.
Let us see some examples of the python isinstance() method.
Example 1: How to use the isinstance() method in python?
1
2
3
4
5
6
7
8
9
class Foo:
a = 5
fooInstance = Foo()
print(isinstance(fooInstance, Foo))
print(isinstance(fooInstance, (list, tuple)))
print(isinstance(fooInstance, (list, tuple, Foo)))
The Output will be as follows:
1
2
3
4
True
False
True
Rules of isinstance()
- True if the object is an instance or subclass of a class or any element of the tuple, False otherwise.
- If classinfo is not a type or tuple of types, a TypeError exception is raised.
This post is licensed under CC BY 4.0 by the author.