Python isinstance() Function – Tutorial with Examples

The isinstance() function in Python is a built-in function that returns a Boolean value indicating whether an object is an instance of a specified class or of a subclass of that class. It is used to check if an object belongs to a particular class or its subclass. The isinstance() function takes two arguments: the first is the object to be checked, and the second is the class or tuple of classes to check against.

Syntax

isinstance(object, classinfo)

Parameters

object – the object to be checked

classinfo – the class or tuple of classes to check against

Return Value

The isinstance() function returns a Boolean value indicating whether the object is an instance of the specified class or of a subclass of that class. If the object is an instance of the specified class or of a subclass of that class, the function returns True, otherwise it returns False.

Examples

Example 1: Basic Usage

class Parent:
    pass

class Child(Parent):
    pass

obj = Child()

print(isinstance(obj, Parent))

Output:

True

In this example, we have defined two classes: Parent and Child. The Child class is defined as a subclass of the Parent class. An object of the Child class is then created, and the isinstance() function is used to check if the object is an instance of the Parent class. The function returns True, indicating that the object is indeed an instance of the Parent class.

Example 2: Multiple Classes

class Grandparent:
    pass

class Parent(Grandparent):
    pass

class Child(Parent):
    pass

obj = Child()

print(isinstance(obj, (Parent, Grandparent)))

Output:

True

In this example, we have defined three classes: Grandparent, Parent, and Child. The Parent class is defined as a subclass of the Grandparent class, and the Child class is defined as a subclass of the Parent class. An object of the Child class is then created, and the isinstance() function is used to check if the object is an instance of either the Parent class or the Grandparent class. The function returns True, indicating that the object is indeed an instance of either the Parent class or the Grandparent class.

Example 3: Using Type() and isinstance()

class Parent:
    pass
class Child(Parent):
    pass

obj = Child()

print(type(obj) == Parent)
print(isinstance(obj, Parent))

Output:

False
True

In this example, we have defined two classes: Parent and Child. The Child class is defined as a subclass of the Parent class. An object of the Child class is then created, and both the type() function and the isinstance() function are used to check if the object is an instance of the Parent class. The type() function returns False, indicating that the object is not of the exact same type as the Parent class. However, the isinstance() function returns True, indicating that the object is indeed an instance of the Parent class or a subclass of the Parent class.

Use Cases

The isinstance() function can be used in a variety of different use cases where it is necessary to check if an object belongs to a particular class or its subclass. Some common use cases are:

  • Checking the type of an object before performing operations on it, to ensure that it is of the expected type.
  • Determining if an object can be used as an argument for a particular function or method, by checking if it is an instance of the required class or its subclass.
  • Debugging and error handling by checking if an object is of the expected type, and raising an error if it is not.

The isinstance() function provides a more flexible and powerful way to check the type of an object than the type() function, as it allows you to check if an object is an instance of a class or its subclass, rather than just checking if it is of the exact same type as the specified class.

In conclusion, the isinstance() function is an essential tool for any Python programmer to check the type of an object and ensure that it is of the expected class or its subclass. With its powerful and flexible capabilities, it is a must-know function for anyone working with Python.

Leave a Reply

Your email address will not be published. Required fields are marked *