Open In App

How to Check the Type of an Object in Python

Last Updated : 29 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore the essential skill of determining the type of an object in Python. Before engaging in any operations on an object within the Python programming language, it is imperative to possess the knowledge of how to check its type. This fundamental task is encountered regularly, whether you are working on a personal project or a complex production system.

Check the Type of an Object in Python

Python offers several methods for identifying an object’s type, with the most basic being the built-in type() function, which provides a quick way to check an object’s type. More advanced techniques, such as the isinstance() function, offer the advantage of handling class hierarchies and inheritance. We will start by going over the built-in type() function and then go on to more advanced methods such as the isinstance() function, the __class__ property, and others.

Get and print an object’s type: type()

type() returns the object’s type. It is similar to a type in other programming languages in that it can be used to get and print the type of a variable or literal.

Python3




print(type('string'))
# <class 'str'>
 
print(type(500))
# <class 'int'>
 
print(type([9, 8, 7]))
# <class 'list'>


Output

<class 'str'>
<class 'int'>
<class 'list'>

Get and print an object’s type: isinstance()

isinstance(object, type) returns True if the object argument is a subclass or instance of the supplied type parameter. Use a tuple as the second option to check for various types. It returns True if the object is an instance of any of the supplied types.

Python3




print(isinstance('string', str))
# True
 
print(isinstance(300, str))
# False
 
print(isinstance(700, (int, str)))
# True


Output

True
False
True

Get and print an object’s type: class__ attribute

In Python, everything is an object, and each object has characteristics of its own. The ‘__class__’ attribute is the only one that can return the class type of the object. The __class__ attribute in Python can also be used to verify object type in addition to built-in functions.

Each Python object has an attribute called __class__ that contains the object’s class information. For example, the class of integer 5 may be found using the code below.

Python3




x = 10
print(x.__class__)
#<type 'int'>
 
y = "GeeksForGeeks"
print(y.__class__)
#<type 'str'>
 
z = 90.0
print(z.__class__)
#<type 'float'>


Output

<class 'int'>
<class 'str'>
<class 'float'>

Get and print an object’s type: ‘==’ operator and type()

Another method for determining an object’s type is to use the type() function and the == operator. This can be used to compare the type of an object to a specific class. For example, to determine whether the integer 5 is of type int, we can use the following code:

Python




x = 5
print(type(x) == int)
# True
y = 5.0
print(type(y) == float)
# True
z = "GeeksforGeeks"
print(type(z) == int)
# False


Output

True
True
False

Remember that Python is a dynamically typed language, which means that the type of a variable might change while it is being used. As a result, it may be necessary to check the kind of an item before doing specified tasks. For example, to add two numbers, we must ensure that they are both integers or floats.

Example

The preceding code demonstrates how a variable’s object type might change during execution.

Python




x = 10
print(type(x))
 
x = "GeeksForGeeks"
print(type(x))


Output

<type 'int'>
<type 'str'>

The initial print statement displayed the type as ‘int’ since 10 is an integer, but after assigning a string to the same variable, the same print statement displayed the type as ‘str’ due to the dynamic change of object type.

Python’s dynamically typed nature necessitates the process of checking object types. For example, if we wish to add two numbers, we must ensure that they are both integers or floats. Incompatible class type operations would result in errors that could only be debugged using functions like type() or the other approach of accessing the ‘__class__’ property.

In summary, mastering object type checking in Python is crucial for developers at all levels, whether you’re a beginner or an experienced programmer. Understanding the techniques, such as type(), isinstance(), and class, is essential for creating efficient and reliable code. These methods provide a versatile toolkit for various type-checking scenarios and will enhance your proficiency in Python development.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads