You are currently viewing Python Type Check with Examples

What’s the canonical way to check for type in Python? In Python, objects and variables can be of different types, such as integers, strings, lists, and dictionaries. Depending on the type of object, certain operations may or may not be valid.

Advertisements

It makes it important to check for the type of an object before performing any operations on it. There are different ways to check for the type of object. In this article, we will discuss methods of checking for type in Python.

In Python, you can check for type using the type() function, the isinstance() function, and comparing the object’s type to a built-in type.

Using the type() function to check for type in Python is considered the most canonical approach. The type() function is specially designed to check for the type of an object. It returns the name of the type that the object belongs to. However there are more ways, let’s see them.

Some people claim that using the isinstance() function is a canonical way, which I think is wrong. The reason is that we don’t know the datatype of an object. However, if we have to compare it with some other type, then their claim maybe is right.

Once again, I would say that there are tons of ways to find out the true type of a python object. Also, I will add that, in different cases, you will expect different types in python.

Strongly Recommended Article: Determine a Python Variable Type

1. type() – Check Type in Python

The type() function is one of the most used methods of checking for type in Python. It returns the type of an object as a type object, which you can use to determine if the object is of a certain type. The type() function takes a single argument, which is the object whose type you want to check.


# Using the type() Function
x = 342
print(type(x))

# Ouptut:
# class 'int'

2. isinstance() Function – Check Type Against Another Type

Python also provides the isinstance() function for checking the type of an object. The isinstance() function takes two arguments: the object whose type you want to check, and the type you want to check against. The function returns True if the object is an instance of the specified type, and False otherwise.


# Using isinstance()
x = 342
print(isinstance(x, int))

# Output:
# True

You can use the isinstance() to check for multiple types at once. To do this, you can pass a tuple of types as the second argument to the isinstance() function.


my_int = 432
print(isinstance(my_int , (int, float, str)))

# Output:
# True

Using isinstance() can be less reliable than using type(), especially when dealing with sub classing.

3. Check for Integer Type in Python

In Python, the integer type is represented by the int class. To check if an object is an integer in Python, you can use the type() function or the isinstance() function.


# Using type() function
x = 42
if type(x) == int:
    print("x is an integer")
else:
    print("x is not an integer")

# Using isinstance() function
y = 3.14
if isinstance(y, int):
    print("y is an integer")
else:
    print("y is not an integer")

In this case, using the isinstance() function to check for integer type is more flexible than using the type() function, as you can also use it to check for subclasses of int. To prove this point, see the below example.


# See how reliable the isinstance() method is?
# MyInt is a subclass of int
class MyInt(int):
    pass

z = MyInt(42)

if isinstance(z, int):
    print("z is an integer")
else:
    print("z is not an integer")
# Output:
# z is an integer

4. Summary and Conclusion

In this article, we learned that why the type() function is a canonical way to check for type in Python. Along that we have seen how the isinstance() function can be reliable and flexible in some cases. Hope this article was helpful. Leave your questions in the comment section.