How to Determine the Type of an Object in Python?

5/5 - (1 vote)
How to Determine the Type of an Object in Python?

Problem Formulation

Every Python object is of a certain type, also called “class”. The class is a blueprint that shows the data and capabilities of each object/instance that is created after this blueprint.

Given a Python object (=instance). How to determine/check/get its type (=class)?

There are many variants of this question:

  • How to determine the type of an object?
  • How to check the type of an instance?
  • How to get the class of an object?
  • How to check the class of an instance?

Let’s start with a short overview how you can answer all of those in the most Pythonic way.

Solution Overview

In Python, the built-in functions type() and isinstance() help you determine the type of an object.

  • type(object) – Returns a string representation of the object’s type.
  • isinstance(object, class) – Returns a Boolean True if the object is an instance of the class, and False otherwise.

Here are two examples to check if an object is a list:

>>> type([1, 2, 3])
<class 'list'>
>>> isinstance([1, 2, 3], list)

You’d use type() to check for the exact type of an object. You’d use isinstance() to confirm your suspicions regarding the type of an object. The function isinstance() is more robust in that you don’t rely on tiny string differences, and it also works with type inheritance. For example, if A inherits from B which inherits from C, an object of type C is also an instance of A.

type() examples with one object argument:

>>> type([1, 2, 3]) is list
>>> type({1, 2, 3}) is dict
>>> type('123') is str
>>> type(0) is int

type() with custom objects:

>>> class Animal:

>>> class Snake(Animal):

>>> a = Animal()
>>> b = Snake()
>>> type(a) is Animal
>>> type(b) is Snake
>>> type(b) is Animal

The third example type(b) is Animal leads us to the following insight:

🌍 Insight: type() returns only the immediate type of an object, but doesn’t work with inheritance.

If you want to overcome this weakness, you should use the isinstance() function that also works for inherited types:

>>> isinstance(Snake(), Animal)

You can also use it on built-in objects and types such as lists, tuples, sets, and dictionaries:

>>> isinstance([1, 2, 3], list)
>>> isinstance((1, 2, 3), tuple)
>>> isinstance({1: 'a', 2: 'b'}, dict)
>>> isinstance({1, 2, 3}, set)

The only use case where you’d prefer type() over isinstance() is if you actually need the type string representation of an object. Otherwise, isinstance() is a more flexible and expressive function.

type() vs isinstance()

The difference between type() and isinstance() is the return value which is a string representation for the former and a Boolean value for the latter. You’d use the type() function if you know nothing. If you already suspect an object to be of a certain type (=class), you can confirm your suspicion by using the isinstance() function.

Python type() Function

Python’s built-in type() function has two purposes. First, you can pass an object as an argument to check the type of this object. Second, you can pass three arguments—name, bases, and dict—to create a new type object that can be used to create instances of this new type.

Python type() Function

Here’s how to use the type() function with one argument to check the type of a given object:

>>> type(42)
<class 'int'>
>>> type('Finxter')
<class 'str'>
type(object)         # Returns class representation of object
ArgumentsobjectObject to be checked for type.
Return ValuestringReturns string representation of the object‘s class.

Related Tutorial: Python type() Function

Python isinstance() Function

Python’s built-in isinstance(object, class) function takes an object and a class as input arguments. It returns True if the object is an instance of the class. Otherwise, it returns False. Instead of a class, you can also pass a tuple of classes to check if the object is an instance of any of the classes in the tuple—such as in isinstance(object, (class_A, class_B, ...)).

Python isinstance() - A Helpful Guide with Examples

Learn by example! Here’s an example on how to use the isinstance() built-in function.

>>> isinstance(42, int)
>>> isinstance(42, (float, list, int))
>>> isinstance('42', (float, list, int))
isinstance(object, class)         # Check if object is instance of class
isinstance(object, (class_A, class_B, ...))         # Check if object is instance of any of the classes in the tuple
ArgumentsobjectObject that should be checked for class membership.
class or tuple of classesClass or tuple of classes this object may or may not belong to.
Return ValueBooleanReturns whether the object belongs to the class (or the classes) or not.