Differences between type() & isinstance() in Python

In this python tutorial, we will be discussing about two different functions type() and isinstance() in Python Programming language. Also we will discuss the differences between type() and isinstance() functions in Python.

Table Of Contents

type() function in Python

The type() function in Python Programming Language is used to check the data type of an object, by proving that object to this function as a parameter. Although the type() function can be used for various purposes. Below are some of the use cases of type() function with examples.

  1. If there is only one argument that is passed to this function. Then it returns the data type of the object passed.

SYNTAX : type(object)

Below is an example of type() function with one parameter.

CODE :

# initialized three variables of three different types of object.
example_one = [2,34,'Python']
example_two = 'Python'
example_three = (1,2,4)

# type() will return the data type of the objects provided as a parameter.
print(type(example_one))
print(type(example_two))
print(type(example_three))

OUTPUT :

<class 'list'>
<class 'str'>
<class 'tuple'>

In the above example, w have seens the the type() function with a single parameter (which can be object of any data type), returns the class type or data type of the object passed.

  1. If there are 3 arguments provided to the type() function, then this will return a new data type. Object, Base and dict are the three arguments, which are passed along with type() in order to create a new type class.

SYNTAX : type(object, bases, dict)

CODE :

# creating a new class type
temp = type('F1',(object, ), dict(p1='Python',p2='Java'))

# printing the temp class.
print(temp)

# type() will print the data type of var temp.
print(type(temp))

OUTPUT :

<class '__main__.F1'>
<class 'type'>

In the above example, the type() function was called with three arguments named as dict, object and Base. Here, it has been used to create a new class type.

  1. type() function in Python programming is also used to check the object type of the parameters. The type() returns True if the conditionals match.

See the example code below:

CODE :

# Initializing different types of variable with different data types.
example_one = [2,34,'Python']
example_two = 'Python'
example_three = (1,2,4)

# Using type() to check data type of the variables.
if type(example_one) is dict:
    print('Dict')
elif type(example_one) is list:
    print('List')
elif type(example_one) is str:
    print('String')

OUTPUT :

List

In the above example, the type() function has been used to check the data type of the variables.

Now we will be discussing isinstance() method then we will be comparing and discussing the differences between both type() function and isinstance() functions.

isinstance() function in Python

As the name suggests, the isinstance() function accepts two arguments, and it is used to check whether the first argument is an instance of the second argument or not. So, basically the isinstance() function in Python is used to check for the class type of a variable. It returns True if the both the arguments are of same class, and return False if both the arguments are of different class.

SYNTAX :

isinstance(object-that-needs-to-be-checked, checked-against-the-class-type)

This also raises an exception to TypeError if the provided class is not a valid one.

See the example code below:

CODE :

# Initializing different types of variable with different data types.
example_one = [2,34,'Python']
example_two = 'Python'
example_three = (1,2,4)

# checking if var example_one is of type dict.
print(isinstance(example_one,dict))

# checking if var example_one is of type list.
print(isinstance(example_one,list))

OUTPUT :

False
True

In the above example, when we tried to check if example_one is of dict type, then isinstance() function returned False. But when we checked if the example_one is of list type, then the isinstance() function returned True. So, we can see how the isinstance() function has been used to check for the data type passed. Now we will learn about the differences between type() and isinstance() in Python.

type() vs isinstance()

The differences between type() and isinstance() are as follows,

  1. The type() returns the type i.e. the data type of given object. Whereas the isnstance() function tells us if the given object is of given type or not.

EXAMPLE CODE:

example_one = [2,34,'Python']

# storing the return value of type() function.
temp = type(example_one)

# printing the data type of var 
# temp which holds the return value of type().
print(type(temp))

# storing the return value of isinstance() function.
temp2 = isinstance(example_one, list)

print(temp2)

# printing the data type of var temp2 which holds the return value of isinstance().
print(type(temp2))

OUTPUT :

<class 'type'>
True
<class 'bool'>
  1. The type() function can be used to create a new type object using arguments named as – Object, Base and Dict as parameters. Whereas isinstance() can be only used to check against the data type.

  2. the type() function can also be used to check class type and name of the class when working with class and objects.

CODE :

# created a class.
class abc:
    print('Isinstance vs type()')

# creating an instance of class.
a = abc()

# using type() to determine the class name of instance a.
print(type(a))

OUTPUT :

Isinstance vs type()
<class '__main__.abc'>

Summary

In this Python tutorial, we discussed the differences between type() and isinstance() function in Python. We discussed type() function and isinstance() function in Python programming language. Also ,we discussed in which case scenario which specific function can be used. The type() function has many use case like, checking the data type of the object, creating new type class object and checking class name. But when talking about checking the data type of the object only, then the isinstance() function should be preferred over the type() function, because it simply returns a Boolean i.e. False if both the data types do not match, or True if the given object is of given type. Thanks.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top