How to use isinstance() Function in Python | isinstance() in Python

isinstance in Python

In this article, we will explore the isinstance() function, which is a built-in function in Python. This function is commonly used by professionals to compare two different data types and determine whether they are the same or not. By using isinstance(), we can easily check if a variable is of a specific data type before proceeding with the program.

Let’s start with an example. Suppose we have a string variable and a class called Animal. The Animal class simply holds the species of the animal. We want to check whether the string is of type string. To do this, we can use the isinstance() method, passing in the string and the data type we want to compare it to. Here’s the code:

string = "Hello, world!"
print(isinstance(string, str))

# Output
# True

In this case, the isinstance() function will return True because the variable string is of type string. However, if we change the value of string to an integer and rerun the program, the function will return False because string is no longer of type string.

The isinstance() function can also be used with other data types, such as integers. You can insert any data type you want to compare. For example, if we run the following code:

string = "Hello, world!"
print(isinstance(string, str))
print(isinstance(string, int))

# Output
# True
# False

The first isinstance() function will return True because string is a string, but the second isinstance() function will return False because string is not an integer.

Things get more interesting when we start combining data types. We can pass a tuple of data types to the isinstance() function to check whether the variable matches any of the specified data types. For example:

string = "Hello, world!"
print(isinstance(string, (int, str)))
print(isinstance(string, (int, float)))

# Output
# True
# False

In the first case, the isinstance() function will return True because string is a string. However, in the second case, it will return False because string is neither an integer nor a float.

The isinstance() function is not limited to built-in data types. We can also use it to compare user-defined types, such as classes. Let’s consider the following example:

class Animal:
    def __init__(self, species):
        self.species = species

class Cat(Animal):
    pass

cat = Cat("Persian")
print(isinstance(cat, Animal))

# Output
# True

In this example, we have a class called Animal and a subclass called Cat. We create an instance of Cat called cat and then use the isinstance() function to check whether cat is of type Animal. The function will return True because cat is indeed an instance of the Animal class.

Conclusion

In this article, we explored the isinstance() function in Python. This built-in function allows us to compare two different data types and determine whether they are the same or not. By using isinstance(), we can easily check if a variable is of a specific data type before proceeding with the program. This function can be especially useful when dealing with user input and ensuring that the correct data type is provided.

If you found this article helpful and insightful, I would greatly appreciate your support. You can show your appreciation by clicking on the button below. Thank you for taking the time to read this article.

Popular Posts

Spread the knowledge
 
  

Leave a Reply

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