note.nkmk.me

Get / determine the type of an object in Python: type(), isinstance()

Posted: 2019-09-16 / Tags: Python

In Python, to get the type of an object or determine whether it is a specific type, use the built-in functions type() and isinstance().

Here, the following contents will be described.

  • Get the type of an object: type()
  • Determine the type of an object: type(), isinstance()
    • With type()
    • With isinstance()
    • The difference between type() and isinstance()
Sponsored Link

Get the type of an object: type()

type() is the function that returns the type of an object passed to argument. You can use this 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'>
source: type.py

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

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

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

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

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

With type()

By comparing the return value of type() with an arbitrary type, you can determine whether the object is 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

If you want to determine if it is one of several types, use in and multiple types of tuples.

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

It is also possible to define functions that change the 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
source: type.py

With isinstance()

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

You can use a tuple as the second argument. Returns True if it is an instance of any type.

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

print(isinstance(100, str))
# False

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

Functions similar to above examples 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

The difference between type() and isinstance()

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

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, but isinstance() returns True also for the superclass.

print(type(derive) is Derive)
# True

print(type(derive) is Base)
# False

print(isinstance(derive, Derive))
# True

print(isinstance(derive, Base))
# True

For example, the boolean type bool (True, False) is a subclass of int. isinstance() returns True for both int and bool for an object of 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 () if you want to determine the exact type, and isinstance() if you want to determine with considering inheritance.

Sponsored Link
Share

Related Categories

Related Posts