Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In Python, the `isinstance()` function is used to check if an object is an instance of a specified class or any of its subclasses. It takes two arguments: the object to be checked and the class or a tuple of classes. The function returns `True` if the object is an instance of the specified class(es) and `False` otherwise, providing a convenient way to perform type checking in Python programs. In this article we will discuss this in detail.
In this article, we will study about isinstance function in Python. We will also study about the syntax, its examples, and its advantages and disadvantages in detail.
The isinstance() function in Python is used to check if an object is an instance of a specified class or any of its subclasses. It takes the object and the class (or a tuple of classes) as arguments and returns True if the object is an instance of the specified class(es), and False otherwise. This function is commonly used for type checking and conditional branching based on object types.
Moving forward, let’s discuss the syntax of Python isinstance function.
Syntax of isinstance
The syntax of Python isinstance function is as below.
isinstance(object, classinfo)
You can also try this code with Online Python Compiler
In the above syntax, the isinstance takes in two variables, object and classinfo. The classinfo is the class we want to check if the object is a part of that classinfo. It returns a boolean value. True if the object is an instance of the class, and False if its not. Let us have a look at some of the examples to understand better.
Examples of Python isinstance Function
The Python isinstance function can be used for various situations. For example,
To check if a variable belongs to a class.
Code
x = 5
if isinstance(x, int):
print("x is an integer")
else:
print("x is not an integer")
You can also try this code with Online Python Compiler
The `isinstance()` function in Python works by checking if an object is an instance of a specified class or any of its subclasses. When you call `isinstance()` with an object and a class (or a tuple of classes) as arguments, it examines the inheritance hierarchy of the object to determine if it belongs to the specified class(es).
Let's see how the `isinstance()` function works internally:
1. The function first checks if the `classinfo` argument is a type or a class. If it is, it directly compares the type of the `object` argument with the `classinfo` using the `type()` function.
2. If the `classinfo` argument is a tuple, the function iterates over each class in the tuple and checks if the `object` is an instance of any of those classes.
3. For each class in the `classinfo` (either a single class or classes in a tuple), the function performs the following steps:
It checks if the class is a direct type of the `object` using the `type()` function. If there is a match, `isinstance()` returns `True`.
If the direct type check fails, the function examines the inheritance hierarchy of the `object`. It traverses through the object's class and its superclasses, comparing each class with the `classinfo` class.
If a match is found anywhere in the inheritance hierarchy, `isinstance()` returns `True`.
4. If the `object` is not an instance of any of the specified classes or their subclasses, `isinstance()` returns `False`.
The `isinstance()` function essentially checks the "is-a" relationship between the object and the specified class(es). It determines if the object is an instance of the class or any of its subclasses based on the inheritance hierarchy.
In this example, `isinstance(dog, Dog)` returns `True` because `dog` is an instance of the `Dog` class. `isinstance(dog, Animal)` also returns `True` because `Dog` is a subclass of `Animal`, so `dog` is considered an instance of `Animal` as well.
On the other hand, `isinstance(dog, Cat)` returns `False` because `dog` is not an instance of the `Cat` class.
When a tuple of classes is provided, such as `isinstance(dog, (Dog, Cat))`, the function checks if `dog` is an instance of either `Dog` or `Cat`, and it returns `True` since `dog` is an instance of `Dog`.
Advantages and Disadvantages of Python isinstance Function
Below are the advantages and disadvantages of Python isinstance function.
Advantages
Flexibility and Polymorphism: The code can handle objects of different classes which share a common interface. With this, we can reuse the code. Also, it simplifies the handling of diverse object types.
Inheritance Awareness: Python isinstance function considers objects to be an instance, This enhances the flexibility of object-oriented programming.
Dynamic Type Checking: Python isinstance function allows dynamic type checking. It adapts to changes in object types and provide flexibility in program structures.
Disadvantages
Complexity and Readability: The code readability is reduced when isinstance is used more often. It can become hard to read and understand the code.
Limited Type Information: It provides information about the type of the object, but it does not provide any information regarding object's attributes,
isinstance checks if an object is an instance of a specific class or a tuple of classes, returning True or False.
What is the difference between the type () and isinstance () in Python?
type() returns the exact type of an object, whereas isinstance() checks if an object is an instance of a class or its subclasses.
How to check isinstance list in Python?
To check if an object is a list in Python, use: isinstance(object, list). This returns True if object is a list.
Conclusion
In this article, we discussed the isinstance function in Python. We also studied about the syntax, its examples, and its advantages and disadvantages. To understand more about object-oriented programming, you can refer to below mentioned article