Get and check the type of an object in Python: type(), isinstance()

Modified: | Tags: Python

In Python, you can get, print, and check the type of an object (variable and literal) with the built-in type() and isinstance() functions.

Instead of checking the type of an object, you can handle exceptions or use the built-in hasattr() function to determine whether an object has the correct methods and attributes. For more information, see the following article.

See the following article for information on how to determine class inheritance relationships and obtain a list of subclasses and superclasses.

Get and print the type of an object: type()

type() returns the type of an object. It can be used to get and print the type of a variable or a literal, similar to typeof in other programming languages.

print(type('string'))
# <class 'str'>

print(type(100))
# <class 'int'>

print(type([0, 1, 2]))
# <class 'list'>
source: type.py

The return value of type() is a type object such as str or int.

print(type(type('string')))
# <class 'type'>

print(type(str))
# <class 'type'>
source: type.py

Check the type of an object: type(), isinstance()

Use type() or isinstance() to check whether an object is of a specific type.

With type()

By comparing the return value of type() with a specific type, you can check whether the object is of that type.

print(type('string') is str)
# True

print(type('string') is int)
# False
source: type.py
def is_str(v):
    return type(v) is str

print(is_str('string'))
# True

print(is_str(100))
# False

print(is_str([0, 1, 2]))
# False
source: type.py

To check if an object is one of several types, use the in keyword combined with a tuple containing multiple types.

def is_str_or_int(v):
    return type(v) in (str, int)

print(is_str_or_int('string'))
# True

print(is_str_or_int(100))
# True

print(is_str_or_int([0, 1, 2]))
# False
source: type.py

You can also define functions that modify their behavior based on the input type.

def type_condition(v):
    if type(v) is str:
        print('type is str')
    elif type(v) is int:
        print('type is int')
    else:
        print('type is not str or int')

type_condition('string')
# type is str

type_condition(100)
# type is int

type_condition([0, 1, 2])
# type is not str or int
source: type.py

With isinstance()

isinstance(object, type) returns True if the object argument is an instance of the specified type argument or a subclass derived from that type.

You can use a tuple as the second argument to check for multiple types. It returns True if the object is an instance of any of the specified types.

print(isinstance('string', str))
# True

print(isinstance(100, str))
# False

print(isinstance(100, (int, str)))
# True

Functions similar to the above examples using type() can be implemented as follows:

def is_str(v):
    return isinstance(v, str)

print(is_str('string'))
# True

print(is_str(100))
# False

print(is_str([0, 1, 2]))
# False
def is_str_or_int(v):
    return isinstance(v, (int, str))

print(is_str_or_int('string'))
# True

print(is_str_or_int(100))
# True

print(is_str_or_int([0, 1, 2]))
# False
def type_condition(v):
    if isinstance(v, str):
        print('type is str')
    elif isinstance(v, int):
        print('type is int')
    else:
        print('type is not str or int')

type_condition('string')
# type is str

type_condition(100)
# type is int

type_condition([0, 1, 2])
# type is not str or int

The difference between type() and isinstance()

The key difference between type() and isinstance() lies in their treatment of inheritance: isinstance() returns True for instances of subclasses inheriting from the specified class, while type() returns True only for exact type matches.

For example, define the following superclass (base class) and subclass (derived class).

class Base:
    pass

class Derive(Base):
    pass

base = Base()
print(type(base))
# <class '__main__.Base'>

derive = Derive()
print(type(derive))
# <class '__main__.Derive'>

type() returns True only when the types match exactly, but isinstance() returns True for instances of both the specified class and its subclasses.

print(type(derive) is Derive)
# True

print(type(derive) is Base)
# False

print(isinstance(derive, Derive))
# True

print(isinstance(derive, Base))
# True

As another example, bool (True and False) is a subclass of int. Thus, in isinstance(), a bool object returns True for both int and bool.

print(type(True))
# <class 'bool'>

print(type(True) is bool)
# True

print(type(True) is int)
# False

print(isinstance(True, bool))
# True

print(isinstance(True, int))
# True

Use type() to check the exact type of an object, and isinstance() to check the type while considering inheritance.

The built-in issubclass() function can be used to check whether a class is a subclass of another class.

print(issubclass(bool, int))
# True

print(issubclass(bool, float))
# False

Related Categories

Related Articles