type(), isinstance() to get and determine type in Python

Money and Business

In Python, the built-in functions type() and isinstance() are used to get and check the type of an object, such as a variable, and to determine whether it is of a particular type.

The following contents are explained here, along with sample code.

  • Obtain and check object type:type()
  • Determination of object type:type(),isinstance()
    • Type determination using type()
    • Type determination using isinstance()
    • Difference between type() and isinstance()

Instead of determining the type of an object, one can use exception handling or the built-in function hasattr() to determine whether an object has the correct methods and attributes.

Obtain and check object type: type()

type(object) is a function that returns the type of the object passed as argument. This can be used to find out the type of an object.

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

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

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

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'>

Determination of object type: type(), isinstance()

Use type() or isinstance() to determine the type.

Type determination using type()

By comparing the return value of type() with an arbitrary type, it can be determined if the object is of any type.

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

print(type('string') is int)
# False
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

If you want to determine if it is one of several types, use the in operator and a tuple or list of several 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

It is also possible to define functions that change processing depending on the argument 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

Type determination using isinstance()

isinstance(object, class) is a function that returns true if the object of the first argument is an instance of the type or subclass of the second argument.

The second argument can be a tuple of types. If it is an instance of either type, true is returned.

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

print(isinstance(100, str))
# False

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

A function similar to the example of type determination using type() can be written 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

Difference between type() and isinstance()

The difference between type() and isinstance() is that isinstance() returns true for instances of subclasses that inherit the class specified as the second argument.

For example, the following superclass (base class) and subclass (derived class) are defined

class Base:
    pass

class Derive(Base):
    pass

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

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

Type determination using type() returns true only when the types match, but isinstance() returns true even for superclasses.

print(type(derive) is Derive)
# True

print(type(derive) is Base)
# False

print(isinstance(derive, Derive))
# True

print(isinstance(derive, Base))
# True

Even for standard types, for example, the boolean type bool (true,false), care must be taken. bool is a subclass of integer type, so isinstance() returns true even for an int from which it is inherited.

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

If you want to determine the exact type, use type(); if you want to determine the type with inheritance taken into account, use isinstance().

The built-in function issubclass() is also provided to determine whether a class is a subclass of another class.

print(issubclass(bool, int))
# True

print(issubclass(bool, float))
# False
Copied title and URL