In this tutorial, we will learn about python isinstance() method and its uses with examples.
The python isinstance() method returns True if the specified object is an instance or subclass; otherwise, it will return False.
The syntax of isinstance() is:
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?
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:
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.
|Was this article helpful?||