DEV Community

hrishikesh1990
hrishikesh1990

Posted on • Originally published at flexiple.com

Various methods in Python to check the type of an object

In this short tutorial, we look at how to use Python to check the type of objects. We take a look at all the various methods that can be used.

This tutorial is a part of our initiative at Flexiple, to write short curated tutorials around often used or interesting concepts.

Table of Contents:

TL;DR - How to check the type of an object in Python?

The type() function takes in an argument and returns the type of the object. Apart from this method the isinstance() function can check if an argument is an instance of a particular class or type.

Python check type:

Unlike most programming languages Python does not require you to specify the data type of a particular object. Python assigns them during compilation. Experienced developers may not have a problem identifying data type, however, beginners might find this a little tricky.

Python facilitates this with the help of two in-built functions. We have explained both these methods in detail below.

Using the type() function:

The type() is used to check the type of an object. This function exists for this exact purpose and is the most commonly used method.

The type() method takes in an argument (object) and returns the class type assigned to the particular object.

The Syntax of type() is as follows:

type(object)
Enter fullscreen mode Exit fullscreen mode

Code to check type using type()

language = "Python"

year =  1991

version = 3.9

print(type(language))
print(type(year))
print(type(version))
Enter fullscreen mode Exit fullscreen mode

The output is as follows:

<class 'str'>
<class 'int'>
<class 'float'>
Enter fullscreen mode Exit fullscreen mode

This is how we use type() to check the type of an object.

Using isinstance():

Isinstance() can also be used to check type. However, unlike the type() function here we pass the classinfo along with the object the function returns a boolean value.

This will be true if the object belongs to a particular class and vice versa. However, do keep in mind that it only returns a boolean value in case it’s true and would not return the correct type in case it is false.

Syntax of isinstance():

isinstance(object, type)
Enter fullscreen mode Exit fullscreen mode

Code using isinstance():

language = "Python"
year =  1991
version = 3.9

print(isinstance(language, str))
print(isinstance(year, int))
print(isinstance(version, int))
Enter fullscreen mode Exit fullscreen mode

The output is as follows:

True
True
False
Enter fullscreen mode Exit fullscreen mode

As the classinfo for version was passed as int, the isinstance method returns a false. This is how you use the isinstance() method.

Closing thoughts

The type() method is the most recommended method to check the type of an object. The isinstance() method stands out as it can be used to check if an object is part of a class as well.

Hence, I would always recommend using the type() method to check the type of an object.

Do let me know your thoughts in the comments section below. :)

Top comments (2)

Collapse
 
aatmaj profile image
Aatmaj

Very nice explanation!

Collapse
 
hrishikesh1990 profile image
hrishikesh1990

Thank you, Aatmaj. :)